orphan
[debian/elilo] / initrd.c
1 /*
2  *  Copyright (C) 2001-2003 Hewlett-Packard Co.
3  *      Contributed by Stephane Eranian <eranian@hpl.hp.com>
4  *
5  * This file is part of the ELILO, the EFI Linux boot loader.
6  *
7  *  ELILO is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  ELILO is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with ELILO; see the file COPYING.  If not, write to the Free
19  *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20  *  02111-1307, USA.
21  *
22  * Please check out the elilo.txt for complete documentation on how
23  * to use this program.
24  */
25
26 #include <efi.h>
27 #include <efilib.h>
28
29 #include "elilo.h"
30
31 /*
32  * This function allocates memory for file image and loads it to memory
33  * OUTPUTS:
34  *      - ELILO_LOAD_SUCCESS: if everything works
35  *      - ELILO_LOAD_ABORTED: in case the user decided to abort loading
36  *      - ELILO_LOAD_ERROR: there was an error during alloc/read
37  *
38  * Adapted from Bill Nottingham <notting@redhat.com>  patch for ELI.
39  */
40 INTN
41 load_file(CHAR16 *filename, memdesc_t *image)
42 {
43         EFI_STATUS status;
44         /*
45          * Actually using the value from sysdeps_initrd_get_addr()
46          * instead of NULL is no change for ia64!
47          */
48         VOID *start_addr = image->start_addr;
49         UINTN pgcnt;
50         UINT64 size = 0;
51         fops_fd_t fd;
52         INTN ret  = ELILO_LOAD_ERROR;
53         
54
55         if (filename == NULL || filename[0] == 0) return -1;
56
57         /* Open the file */
58         status = fops_open(filename, &fd);
59         if (EFI_ERROR(status)) {
60                 ERR_PRT((L"Open file %s failed: %r", filename, status));
61                 return -1;
62         }
63
64         DBG_PRT((L"open %s worked", filename));
65
66         /* warning: this function allocates memory  */
67         status = fops_infosize(fd, &size);
68         if (EFI_ERROR(status)) {
69                 ERR_PRT((L"Couldn't read file %s info %r", filename, status));
70                 goto error;
71         }
72         
73         image->size = size;
74
75         /* round up to get required number of pages (4KB) */
76         image->pgcnt = pgcnt = EFI_SIZE_TO_PAGES(image->size);
77
78         start_addr = alloc_pages(pgcnt, EfiLoaderData,
79                 start_addr ? AllocateAddress : AllocateAnyPages, start_addr);
80
81         start_addr = sysdeps_checkfix_initrd(start_addr, image);
82
83         if (start_addr == NULL) {
84                 ERR_PRT((L"Failed to allocate %d pages for %s image", pgcnt,
85                          filename));
86                 goto error;
87         }
88         VERB_PRT(2, Print(L"%s image: total_size: %d bytes base: " PTR_FMT " "
89                           "pages %d\n", filename, image->size,
90                           start_addr, pgcnt));
91
92         Print(L"Loading file %s...", filename);
93
94         ret = read_file(fd, image->size, start_addr);   
95
96         fops_close(fd);
97
98         if (ret != ELILO_LOAD_SUCCESS) {
99                 ERR_PRT((L"read image(%s) failed: %d", filename, ret));
100                 goto error;
101         }
102
103         Print(L"done\n");
104
105         image->start_addr = start_addr;
106
107         return ELILO_LOAD_SUCCESS;
108
109 error:
110         if (start_addr) free(start_addr);
111
112         /*
113          * make sure nothing is passed to kernel
114          * in case of error.
115          */
116         image->start_addr = 0;
117         image->pgcnt      = 0;
118         image->size       = 0;
119
120         return ret;
121 }
122
123