Imported Debian patch 3.12-4.1
[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         VOID *start_addr = NULL;
45         UINTN pgcnt;
46         UINT64 size = 0;
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 file %s failed: %r", filename, status));
57                 return -1;
58         }
59
60         DBG_PRT((L"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 file %s info %r", filename, status));
66                 goto error;
67         }
68         
69         image->size = size;
70
71         /* round up to get required number of pages (4KB) */
72         image->pgcnt = pgcnt = EFI_SIZE_TO_PAGES(image->size);
73
74         start_addr = alloc_pages(pgcnt, EfiLoaderData, start_addr ? AllocateAddress : AllocateAnyPages, 0 );
75         if (start_addr == NULL) {
76                 ERR_PRT((L"Failed to allocate %d pages for %s image", pgcnt,
77                          filename));
78                 goto error;
79         }
80         VERB_PRT(2, Print(L"%s image: total_size: %d bytes base: " PTR_FMT " "
81                           "pages %d\n", filename, image->size,
82                           start_addr, pgcnt));
83
84         Print(L"Loading file %s...", filename);
85
86         ret = read_file(fd, image->size, start_addr);   
87
88         fops_close(fd);
89
90         if (ret != ELILO_LOAD_SUCCESS) {
91                 ERR_PRT((L"read image(%s) failed: %d", filename, ret));
92                 goto error;
93         }
94
95         Print(L"done\n");
96
97         image->start_addr = start_addr;
98
99         return ELILO_LOAD_SUCCESS;
100
101 error:
102         if (start_addr) free(start_addr);
103
104         /*
105          * make sure nothing is passed to kernel
106          * in case of error.
107          */
108         image->start_addr = 0;
109         image->pgcnt      = 0;
110         image->size       = 0;
111
112         return ret;
113 }
114
115