2004-02-23 13:11:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001-2003 Hewlett-Packard Co.
|
|
|
|
* Contributed by Stephane Eranian <eranian@hpl.hp.com>
|
|
|
|
*
|
|
|
|
* This file is part of the ELILO, the EFI Linux boot loader.
|
|
|
|
*
|
|
|
|
* ELILO is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* ELILO is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with ELILO; see the file COPYING. If not, write to the Free
|
|
|
|
* Software Foundation, 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Please check out the elilo.txt for complete documentation on how
|
|
|
|
* to use this program.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <efi.h>
|
|
|
|
#include <efilib.h>
|
|
|
|
|
|
|
|
#include "elilo.h"
|
|
|
|
|
|
|
|
/*
|
2005-12-06 13:21:36 +00:00
|
|
|
* This function allocates memory for file image and loads it to memory
|
2004-02-23 13:11:30 +00:00
|
|
|
* OUTPUTS:
|
|
|
|
* - ELILO_LOAD_SUCCESS: if everything works
|
|
|
|
* - ELILO_LOAD_ABORTED: in case the user decided to abort loading
|
|
|
|
* - ELILO_LOAD_ERROR: there was an error during alloc/read
|
|
|
|
*
|
|
|
|
* Adapted from Bill Nottingham <notting@redhat.com> patch for ELI.
|
|
|
|
*/
|
|
|
|
INTN
|
2005-12-06 13:21:36 +00:00
|
|
|
load_file(CHAR16 *filename, memdesc_t *image)
|
2004-02-23 13:11:30 +00:00
|
|
|
{
|
|
|
|
EFI_STATUS status;
|
2005-12-06 13:21:36 +00:00
|
|
|
VOID *start_addr = image->start_addr;
|
2004-02-23 13:11:30 +00:00
|
|
|
UINTN pgcnt;
|
2005-12-06 13:21:36 +00:00
|
|
|
UINT64 size = 0;
|
2004-02-23 13:11:30 +00:00
|
|
|
fops_fd_t fd;
|
|
|
|
INTN ret = ELILO_LOAD_ERROR;
|
|
|
|
|
|
|
|
|
|
|
|
if (filename == NULL || filename[0] == 0) return -1;
|
|
|
|
|
|
|
|
/* Open the file */
|
|
|
|
status = fops_open(filename, &fd);
|
|
|
|
if (EFI_ERROR(status)) {
|
2005-12-06 13:21:36 +00:00
|
|
|
ERR_PRT((L"Open file %s failed: %r", filename, status));
|
2004-02-23 13:11:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-06 13:21:36 +00:00
|
|
|
DBG_PRT((L"open %s worked", filename));
|
2004-02-23 13:11:30 +00:00
|
|
|
|
|
|
|
/* warning: this function allocates memory */
|
|
|
|
status = fops_infosize(fd, &size);
|
|
|
|
if (EFI_ERROR(status)) {
|
2005-12-06 13:21:36 +00:00
|
|
|
ERR_PRT((L"Couldn't read file %s info %r", filename, status));
|
2004-02-23 13:11:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2005-12-06 13:21:36 +00:00
|
|
|
image->size = size;
|
2004-02-23 13:11:30 +00:00
|
|
|
|
2005-12-06 13:21:36 +00:00
|
|
|
/* round up to get required number of pages (4KB) */
|
|
|
|
image->pgcnt = pgcnt = EFI_SIZE_TO_PAGES(image->size);
|
2004-02-23 13:11:30 +00:00
|
|
|
|
|
|
|
start_addr = alloc_pages(pgcnt, EfiLoaderData, start_addr ? AllocateAddress : AllocateAnyPages, start_addr);
|
|
|
|
if (start_addr == NULL) {
|
2005-12-06 13:21:36 +00:00
|
|
|
ERR_PRT((L"Failed to allocate %d pages for %s image", pgcnt,
|
|
|
|
filename));
|
2004-02-23 13:11:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2005-12-06 13:21:36 +00:00
|
|
|
VERB_PRT(2, Print(L"%s image: total_size: %ld bytes base: 0x%lx "
|
|
|
|
"pages %d\n", filename, image->size,
|
|
|
|
(UINTN)start_addr, pgcnt));
|
2004-02-23 13:11:30 +00:00
|
|
|
|
2005-12-06 13:21:36 +00:00
|
|
|
Print(L"Loading file %s...", filename);
|
2004-02-23 13:11:30 +00:00
|
|
|
|
2005-12-06 13:21:36 +00:00
|
|
|
ret = read_file(fd, image->size, start_addr);
|
2004-02-23 13:11:30 +00:00
|
|
|
|
|
|
|
fops_close(fd);
|
|
|
|
|
|
|
|
if (ret != ELILO_LOAD_SUCCESS) {
|
2005-12-06 13:21:36 +00:00
|
|
|
ERR_PRT((L"read image(%s) failed: %d", filename, ret));
|
2004-02-23 13:11:30 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Print(L"done\n");
|
|
|
|
|
2005-12-06 13:21:36 +00:00
|
|
|
image->start_addr = start_addr;
|
2004-02-23 13:11:30 +00:00
|
|
|
|
|
|
|
return ELILO_LOAD_SUCCESS;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (start_addr) free(start_addr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make sure nothing is passed to kernel
|
|
|
|
* in case of error.
|
|
|
|
*/
|
2005-12-06 13:21:36 +00:00
|
|
|
image->start_addr = 0;
|
|
|
|
image->pgcnt = 0;
|
|
|
|
image->size = 0;
|
2004-02-23 13:11:30 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|