hw/i386/pc: format load_linux function

Signed-off-by: liguang <lig.fnst@cn.fujitsu.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
liguang 2013-03-26 16:43:19 +08:00 committed by Stefan Hajnoczi
parent 9941afdef4
commit 0f9d76e5a9
1 changed files with 56 additions and 53 deletions

View File

@ -646,8 +646,8 @@ static long get_file_size(FILE *f)
static void load_linux(void *fw_cfg, static void load_linux(void *fw_cfg,
const char *kernel_filename, const char *kernel_filename,
const char *initrd_filename, const char *initrd_filename,
const char *kernel_cmdline, const char *kernel_cmdline,
hwaddr max_ram_size) hwaddr max_ram_size)
{ {
uint16_t protocol; uint16_t protocol;
@ -664,60 +664,62 @@ static void load_linux(void *fw_cfg,
/* load the kernel header */ /* load the kernel header */
f = fopen(kernel_filename, "rb"); f = fopen(kernel_filename, "rb");
if (!f || !(kernel_size = get_file_size(f)) || if (!f || !(kernel_size = get_file_size(f)) ||
fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) != fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
MIN(ARRAY_SIZE(header), kernel_size)) { MIN(ARRAY_SIZE(header), kernel_size)) {
fprintf(stderr, "qemu: could not load kernel '%s': %s\n", fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
kernel_filename, strerror(errno)); kernel_filename, strerror(errno));
exit(1); exit(1);
} }
/* kernel protocol version */ /* kernel protocol version */
#if 0 #if 0
fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202)); fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
#endif #endif
if (ldl_p(header+0x202) == 0x53726448) if (ldl_p(header+0x202) == 0x53726448) {
protocol = lduw_p(header+0x206); protocol = lduw_p(header+0x206);
else { } else {
/* This looks like a multiboot kernel. If it is, let's stop /* This looks like a multiboot kernel. If it is, let's stop
treating it like a Linux kernel. */ treating it like a Linux kernel. */
if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename, if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
kernel_cmdline, kernel_size, header)) kernel_cmdline, kernel_size, header)) {
return; return;
protocol = 0; }
protocol = 0;
} }
if (protocol < 0x200 || !(header[0x211] & 0x01)) { if (protocol < 0x200 || !(header[0x211] & 0x01)) {
/* Low kernel */ /* Low kernel */
real_addr = 0x90000; real_addr = 0x90000;
cmdline_addr = 0x9a000 - cmdline_size; cmdline_addr = 0x9a000 - cmdline_size;
prot_addr = 0x10000; prot_addr = 0x10000;
} else if (protocol < 0x202) { } else if (protocol < 0x202) {
/* High but ancient kernel */ /* High but ancient kernel */
real_addr = 0x90000; real_addr = 0x90000;
cmdline_addr = 0x9a000 - cmdline_size; cmdline_addr = 0x9a000 - cmdline_size;
prot_addr = 0x100000; prot_addr = 0x100000;
} else { } else {
/* High and recent kernel */ /* High and recent kernel */
real_addr = 0x10000; real_addr = 0x10000;
cmdline_addr = 0x20000; cmdline_addr = 0x20000;
prot_addr = 0x100000; prot_addr = 0x100000;
} }
#if 0 #if 0
fprintf(stderr, fprintf(stderr,
"qemu: real_addr = 0x" TARGET_FMT_plx "\n" "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
"qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n" "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
"qemu: prot_addr = 0x" TARGET_FMT_plx "\n", "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
real_addr, real_addr,
cmdline_addr, cmdline_addr,
prot_addr); prot_addr);
#endif #endif
/* highest address for loading the initrd */ /* highest address for loading the initrd */
if (protocol >= 0x203) if (protocol >= 0x203) {
initrd_max = ldl_p(header+0x22c); initrd_max = ldl_p(header+0x22c);
else } else {
initrd_max = 0x37ffffff; initrd_max = 0x37ffffff;
}
if (initrd_max >= max_ram_size-ACPI_DATA_SIZE) if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
initrd_max = max_ram_size-ACPI_DATA_SIZE-1; initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
@ -727,10 +729,10 @@ static void load_linux(void *fw_cfg,
fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline); fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
if (protocol >= 0x202) { if (protocol >= 0x202) {
stl_p(header+0x228, cmdline_addr); stl_p(header+0x228, cmdline_addr);
} else { } else {
stw_p(header+0x20, 0xA33F); stw_p(header+0x20, 0xA33F);
stw_p(header+0x22, cmdline_addr-real_addr); stw_p(header+0x22, cmdline_addr-real_addr);
} }
/* handle vga= parameter */ /* handle vga= parameter */
@ -755,23 +757,23 @@ static void load_linux(void *fw_cfg,
/* High nybble = B reserved for QEMU; low nybble is revision number. /* High nybble = B reserved for QEMU; low nybble is revision number.
If this code is substantially changed, you may want to consider If this code is substantially changed, you may want to consider
incrementing the revision. */ incrementing the revision. */
if (protocol >= 0x200) if (protocol >= 0x200) {
header[0x210] = 0xB0; header[0x210] = 0xB0;
}
/* heap */ /* heap */
if (protocol >= 0x201) { if (protocol >= 0x201) {
header[0x211] |= 0x80; /* CAN_USE_HEAP */ header[0x211] |= 0x80; /* CAN_USE_HEAP */
stw_p(header+0x224, cmdline_addr-real_addr-0x200); stw_p(header+0x224, cmdline_addr-real_addr-0x200);
} }
/* load initrd */ /* load initrd */
if (initrd_filename) { if (initrd_filename) {
if (protocol < 0x200) { if (protocol < 0x200) {
fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n"); fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
exit(1); exit(1);
} }
initrd_size = get_image_size(initrd_filename); initrd_size = get_image_size(initrd_filename);
if (initrd_size < 0) { if (initrd_size < 0) {
fprintf(stderr, "qemu: error reading initrd %s\n", fprintf(stderr, "qemu: error reading initrd %s\n",
initrd_filename); initrd_filename);
@ -787,14 +789,15 @@ static void load_linux(void *fw_cfg,
fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size); fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size); fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
stl_p(header+0x218, initrd_addr); stl_p(header+0x218, initrd_addr);
stl_p(header+0x21c, initrd_size); stl_p(header+0x21c, initrd_size);
} }
/* load kernel and setup */ /* load kernel and setup */
setup_size = header[0x1f1]; setup_size = header[0x1f1];
if (setup_size == 0) if (setup_size == 0) {
setup_size = 4; setup_size = 4;
}
setup_size = (setup_size+1)*512; setup_size = (setup_size+1)*512;
kernel_size -= setup_size; kernel_size -= setup_size;