Imported Debian patch 3.4-9
[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 the initial ramdisk (initrd) 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_initrd(CHAR16 *filename, memdesc_t *initrd)
42 {
43         EFI_STATUS status;
44         VOID *start_addr = initrd->start_addr;
45         UINT64 size = 0;
46         UINTN pgcnt;
47         fops_fd_t fd;
48         INTN ret  = ELILO_LOAD_ERROR;
49         
50
51         if (filename == NULL || filename[0] == 0) return -1;
52
53         /* Open the file */
54         status = fops_open(filename, &fd);
55         if (EFI_ERROR(status)) {
56                 ERR_PRT((L"Open initrd file %s failed: %r", filename, status));
57                 return -1;
58         }
59
60         DBG_PRT((L"initrd_open %s worked", filename));
61
62         /* warning: this function allocates memory  */
63         status = fops_infosize(fd, &size);
64         if (EFI_ERROR(status)) {
65                 ERR_PRT((L"Couldn't read initrd file %s info %r",filename, status));
66                 goto error;
67         }
68         
69         /* round up to get required number of pages (4KB) */
70         initrd->pgcnt = pgcnt = EFI_SIZE_TO_PAGES(size);
71
72
73
74         start_addr = alloc_pages(pgcnt, EfiLoaderData, start_addr ? AllocateAddress : AllocateAnyPages, start_addr);
75         if (start_addr == NULL) {
76                 ERR_PRT((L"Failed to allocate %d pages for initrd", pgcnt));
77                 goto error;
78         }
79         VERB_PRT(2, Print(L"initrd: total_size: %ld bytes base: 0x%lx pages %d\n", 
80                         size, (UINT64)start_addr, pgcnt));
81
82         Print(L"Loading initrd %s...", filename);
83
84         ret = read_file(fd, size, start_addr);  
85
86         fops_close(fd);
87
88         if (ret != ELILO_LOAD_SUCCESS) {
89                 ERR_PRT((L"read initrd(%s) failed: %d", filename, ret));
90                 goto error;
91         }
92
93         Print(L"done\n");
94
95         initrd->start_addr = start_addr;
96
97         return ELILO_LOAD_SUCCESS;
98
99 error:
100         if (start_addr) free(start_addr);
101
102         /*
103          * make sure nothing is passed to kernel
104          * in case of error.
105          */
106         initrd->start_addr = 0;
107         initrd->pgcnt      = 0;
108
109         return ret;
110 }
111
112