2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
5 * This file is part of the ELILO, the EFI Linux boot loader.
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)
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.
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
22 * Please check out the elilo.txt for complete documentation on how
23 * to use this program.
32 * This function allocates memory for file image and loads it to memory
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
38 * Adapted from Bill Nottingham <notting@redhat.com> patch for ELI.
41 load_file(CHAR16 *filename, memdesc_t *image)
45 * Actually using the value from sysdeps_initrd_get_addr()
46 * instead of NULL is no change for ia64!
48 VOID *start_addr = image->start_addr;
52 INTN ret = ELILO_LOAD_ERROR;
55 if (filename == NULL || filename[0] == 0) return -1;
58 status = fops_open(filename, &fd);
59 if (EFI_ERROR(status)) {
60 ERR_PRT((L"Open file %s failed: %r", filename, status));
64 DBG_PRT((L"open %s worked", filename));
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));
75 /* round up to get required number of pages (4KB) */
76 image->pgcnt = pgcnt = EFI_SIZE_TO_PAGES(image->size);
78 start_addr = alloc_pages(pgcnt, EfiLoaderData,
79 start_addr ? AllocateAddress : AllocateAnyPages, start_addr);
81 start_addr = sysdeps_checkfix_initrd(start_addr, image);
83 if (start_addr == NULL) {
84 ERR_PRT((L"Failed to allocate %d pages for %s image", pgcnt,
88 VERB_PRT(2, Print(L"%s image: total_size: %d bytes base: " PTR_FMT " "
89 "pages %d\n", filename, image->size,
92 Print(L"Loading file %s...", filename);
94 ret = read_file(fd, image->size, start_addr);
98 if (ret != ELILO_LOAD_SUCCESS) {
99 ERR_PRT((L"read image(%s) failed: %d", filename, ret));
105 image->start_addr = start_addr;
107 return ELILO_LOAD_SUCCESS;
110 if (start_addr) free(start_addr);
113 * make sure nothing is passed to kernel
116 image->start_addr = 0;