1 /*
2  *   Creation Date: <2002/10/02 22:24:24 samuel>
3  *   Time-stamp: <2004/03/27 01:57:55 samuel>
4  *
5  *	<main.c>
6  *
7  *
8  *
9  *   Copyright (C) 2002, 2003, 2004 Samuel Rydh (samuel@ibrium.se)
10  *
11  *   This program is free software; you can redistribute it and/or
12  *   modify it under the terms of the GNU General Public License
13  *   as published by the Free Software Foundation
14  *
15  */
16 
17 #include "config.h"
18 #include "libopenbios/bindings.h"
19 #include "libopenbios/elf_load.h"
20 #include "arch/common/nvram.h"
21 #include "packages/nvram.h"
22 #include "libc/diskio.h"
23 #include "libc/vsprintf.h"
24 #include "kernel.h"
25 #include "drivers/drivers.h"
26 #include "libopenbios/ofmem.h"
27 #include "libopenbios/initprogram.h"
28 #include "context.h"
29 #define NO_QEMU_PROTOS
30 #include "arch/common/fw_cfg.h"
31 
32 //#define DEBUG_QEMU
33 
34 #ifdef DEBUG_QEMU
35 #define SUBSYS_DPRINTF(subsys, fmt, args...) \
36     do { printk("%s - %s: " fmt, subsys, __func__ , ##args); } while (0)
37 #else
38 #define SUBSYS_DPRINTF(subsys, fmt, args...) \
39     do { } while (0)
40 #endif
41 #define CHRP_DPRINTF(fmt, args...) SUBSYS_DPRINTF("CHRP", fmt, ##args)
42 #define ELF_DPRINTF(fmt, args...) SUBSYS_DPRINTF("ELF", fmt, ##args)
43 #define NEWWORLD_DPRINTF(fmt, args...) SUBSYS_DPRINTF("NEWWORLD", fmt, ##args)
44 
check_preloaded_kernel(void)45 static void check_preloaded_kernel(void)
46 {
47     unsigned long kernel_image, kernel_size;
48     unsigned long initrd_image, initrd_size;
49     const char * kernel_cmdline;
50     volatile struct context *ctx = __context;
51 
52     kernel_size = fw_cfg_read_i32(FW_CFG_KERNEL_SIZE);
53     if (kernel_size) {
54         kernel_image = fw_cfg_read_i32(FW_CFG_KERNEL_ADDR);
55         kernel_cmdline = (const char *)(uintptr_t) fw_cfg_read_i32(FW_CFG_KERNEL_CMDLINE);
56         initrd_image = fw_cfg_read_i32(FW_CFG_INITRD_ADDR);
57         initrd_size = fw_cfg_read_i32(FW_CFG_INITRD_SIZE);
58         printk("[ppc] Kernel already loaded (0x%8.8lx + 0x%8.8lx) "
59                "(initrd 0x%8.8lx + 0x%8.8lx)\n",
60                kernel_image, kernel_size, initrd_image, initrd_size);
61         if (kernel_cmdline) {
62                phandle_t ph;
63 	       printk("[ppc] Kernel command line: %s\n", kernel_cmdline);
64 	       ph = find_dev("/chosen");
65                set_property(ph, "bootargs", strdup(kernel_cmdline), strlen(kernel_cmdline) + 1);
66         }
67 
68         arch_init_program();
69         ctx->regs[REG_R3] = initrd_image;
70         ctx->regs[REG_R4] = initrd_size;
71         ctx->pc = kernel_image;
72 
73         start_elf();
74     }
75 }
76 
77 /************************************************************************/
78 /*	entry								*/
79 /************************************************************************/
80 
81 void
boot(void)82 boot( void )
83 {
84         uint16_t boot_device = fw_cfg_read_i16(FW_CFG_BOOT_DEVICE);
85 
86 	fword("update-chosen");
87 	if (boot_device == 'm') {
88 	        check_preloaded_kernel();
89 	}
90 
91 	if (is_apple()) {
92 		update_nvram();
93 	}
94 }
95