2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (C) 2006 Semihalf, Piotr Kruszynski <ppk@semihalf.com>
4 * Copyright (C) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/reboot.h>
35 #include <sys/linker.h>
37 #include <machine/elf.h>
38 #include <machine/metadata.h>
39 #include <machine/bootinfo.h>
41 #include "api_public.h"
42 #include "bootstrap.h"
46 * Return a 'boothowto' value corresponding to the kernel arguments in
47 * (kargs) and any relevant environment variables.
54 {"boot_askname", RB_ASKNAME},
55 {"boot_cdrom", RB_CDROM},
57 {"boot_dfltroot", RB_DFLTROOT},
59 {"boot_multicons", RB_MULTIPLE},
60 {"boot_mute", RB_MUTE},
61 {"boot_pause", RB_PAUSE},
62 {"boot_serial", RB_SERIAL},
63 {"boot_single", RB_SINGLE},
64 {"boot_verbose", RB_VERBOSE},
69 md_getboothowto(char *kargs)
82 if (!active && (*cp == '-'))
111 howto |= RB_DFLTROOT;
127 /* get equivalents from the environment */
128 for (i = 0; howto_names[i].ev != NULL; i++) {
129 if (getenv(howto_names[i].ev) != NULL)
130 howto |= howto_names[i].mask;
132 if (!strcmp(getenv("console"), "comconsole"))
134 if (!strcmp(getenv("console"), "nullconsole"))
141 * Copy the environment into the load area starting at (addr).
142 * Each variable is formatted as <name>=<value>, with a single nul
143 * separating each variable, and a double nul terminating the environment.
146 md_copyenv(vm_offset_t addr)
150 /* traverse the environment */
151 for (ep = environ; ep != NULL; ep = ep->ev_next) {
152 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
153 addr += strlen(ep->ev_name);
154 archsw.arch_copyin("=", addr, 1);
156 if (ep->ev_value != NULL) {
157 archsw.arch_copyin(ep->ev_value, addr,
158 strlen(ep->ev_value));
159 addr += strlen(ep->ev_value);
161 archsw.arch_copyin("", addr, 1);
164 archsw.arch_copyin("", addr, 1);
170 * Copy module-related data into the load area, where it can be
171 * used as a directory for loaded modules.
173 * Module data is presented in a self-describing format. Each datum
174 * is preceded by a 32-bit identifier and a 32-bit size field.
176 * Currently, the following data are saved:
178 * MOD_NAME (variable) module name (string)
179 * MOD_TYPE (variable) module type (string)
180 * MOD_ARGS (variable) module parameters (string)
181 * MOD_ADDR sizeof(vm_offset_t) module load address
182 * MOD_SIZE sizeof(size_t) module size
183 * MOD_METADATA (variable) type-specific metadata
185 #define COPY32(v, a, c) { \
188 archsw.arch_copyin(&x, a, sizeof(x)); \
192 #define MOD_STR(t, a, s, c) { \
194 COPY32(strlen(s) + 1, a, c) \
196 archsw.arch_copyin(s, a, strlen(s) + 1);\
197 a += roundup(strlen(s) + 1, sizeof(u_long));\
200 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
201 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
202 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
204 #define MOD_VAR(t, a, s, c) { \
206 COPY32(sizeof(s), a, c); \
208 archsw.arch_copyin(&s, a, sizeof(s)); \
209 a += roundup(sizeof(s), sizeof(u_long)); \
212 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
213 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
215 #define MOD_METADATA(a, mm, c) { \
216 COPY32(MODINFO_METADATA | mm->md_type, a, c);\
217 COPY32(mm->md_size, a, c); \
219 archsw.arch_copyin(mm->md_data, a, mm->md_size);\
220 a += roundup(mm->md_size, sizeof(u_long)); \
223 #define MOD_END(a, c) { \
224 COPY32(MODINFO_END, a, c); \
229 md_copymodules(vm_offset_t addr)
231 struct preloaded_file *fp;
232 struct file_metadata *md;
237 /* start with the first module on the list, should be the kernel */
238 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
240 MOD_NAME(addr, fp->f_name, c); /* this field must be first */
241 MOD_TYPE(addr, fp->f_type, c);
243 MOD_ARGS(addr, fp->f_args, c);
244 a = fp->f_addr - __elfN(relocation_offset);
245 MOD_ADDR(addr, a, c);
246 MOD_SIZE(addr, fp->f_size, c);
247 for (md = fp->f_metadata; md != NULL; md = md->md_next) {
248 if (!(md->md_type & MODINFOMD_NOCOPY))
249 MOD_METADATA(addr, md, c);
257 * Prepare the bootinfo structure. Put a ptr to the allocated struct in addr,
261 md_bootinfo(struct bootinfo **addr)
263 #define TMP_MAX_ETH 8
266 struct bi_mem_region tmp_mr[TMP_MAX_MR];
267 struct bi_eth_addr tmp_eth[TMP_MAX_ETH];
272 u_int8_t tmp_addr[6];
273 int i, n, mr_no, eth_no, size;
275 if ((si = ub_get_sys_info()) == NULL)
276 panic("can't retrieve U-Boot sysinfo");
279 * Handle mem regions (we only care about DRAM)
281 for (i = 0, mr_no = 0; i < si->mr_no; i++) {
282 if (si->mr[i].flags == MR_ATTR_DRAM) {
283 if (mr_no >= TMP_MAX_MR) {
284 printf("too many memory regions: %d\n", mr_no);
287 tmp_mr[mr_no].mem_base = si->mr[i].start;
288 tmp_mr[mr_no].mem_size = si->mr[i].size;
294 panic("can't retrieve RAM info");
296 size = (mr_no * sizeof(struct bi_mem_region) - sizeof(bi->bi_data));
299 * Handle Ethernet addresses: parse u-boot env for eth%daddr
303 while ((env = ub_env_enum(env)) != NULL) {
304 if (strncmp(env, "eth", 3) == 0 &&
305 strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
307 /* Extract interface number */
308 i = strtol(env + 3, &end, 10);
309 if (end == (env + 3))
310 /* 'ethaddr' means interface 0 address */
315 if (n >= TMP_MAX_MR) {
316 printf("Ethernet interface number too high: %d. "
321 str = ub_env_get(env);
322 for (i = 0; i < 6; i++) {
323 tmp_addr[i] = str ? strtol(str, &end, 16) : 0;
325 str = (*end) ? end + 1 : end;
327 tmp_eth[n].mac_addr[i] = tmp_addr[i];
330 /* eth_no is 1-based number of all interfaces defined */
336 size += (eth_no * sizeof(struct bi_eth_addr)) + sizeof(struct bootinfo);
339 * Once its whole size is calculated, allocate space for the bootinfo
340 * and copy over the contents from temp containers.
342 if ((bi = malloc(size)) == NULL)
343 panic("can't allocate mem for bootinfo");
345 ptr = (struct bi_mem_region *)bi->bi_data;
346 bcopy(tmp_mr, ptr, mr_no * sizeof(struct bi_mem_region));
347 ptr += mr_no * sizeof(struct bi_mem_region);
348 bcopy(tmp_eth, ptr, eth_no * sizeof(struct bi_eth_addr));
350 bi->bi_mem_reg_no = mr_no;
351 bi->bi_eth_addr_no = eth_no;
352 bi->bi_version = BI_VERSION;
353 bi->bi_bar_base = si->bar;
354 bi->bi_cpu_clk = si->clk_cpu;
355 bi->bi_bus_clk = si->clk_bus;
363 * Load the information expected by a powerpc kernel.
365 * - The 'boothowto' argument is constructed
366 * - The 'bootdev' argument is constructed
367 * - The kernel environment is copied into kernel space.
368 * - Module metadata are formatted and placed in kernel space.
371 md_load(char *args, vm_offset_t *modulep)
373 struct preloaded_file *kfp;
374 struct preloaded_file *xp;
375 struct file_metadata *md;
376 struct bootinfo *bip;
388 * These metadata addreses must be converted for kernel after
392 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND, MODINFOMD_ENVP
395 howto = md_getboothowto(args);
398 * Allow the environment variable 'rootdev' to override the supplied
399 * device. This should perhaps go to MI code and/or have $rootdev
400 * tested/set by MI code before launching the kernel.
402 rootdevname = getenv("rootdev");
403 if (rootdevname == NULL)
404 rootdevname = getenv("currdev");
405 /* Try reading the /etc/fstab file to select the root device */
406 getrootmount(rootdevname);
408 /* find the last module in the chain */
410 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
411 if (addr < (xp->f_addr + xp->f_size))
412 addr = xp->f_addr + xp->f_size;
414 /* pad to a page boundary */
415 addr = roundup(addr, PAGE_SIZE);
417 /* copy our environment */
419 addr = md_copyenv(addr);
421 /* pad to a page boundary */
422 addr = roundup(addr, PAGE_SIZE);
424 /* prepare bootinfo */
425 bisize = md_bootinfo(&bip);
428 kfp = file_findfile(NULL, "elf32 kernel");
430 kfp = file_findfile(NULL, "elf kernel");
432 panic("can't find kernel file");
433 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
434 file_addmetadata(kfp, MODINFOMD_BOOTINFO, bisize, bip);
435 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
436 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
439 size = md_copymodules(0);
440 kernend = roundup(addr + size, PAGE_SIZE);
442 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
443 bcopy(&kernend, md->md_data, sizeof kernend);
445 /* Convert addresses to the final VA */
446 *modulep -= __elfN(relocation_offset);
448 for (i = 0; i < sizeof mdt / sizeof mdt[0]; i++) {
449 md = file_findmetadata(kfp, mdt[i]);
451 bcopy(md->md_data, &vaddr, sizeof vaddr);
452 vaddr -= __elfN(relocation_offset);
453 bcopy(&vaddr, md->md_data, sizeof vaddr);
456 (void)md_copymodules(addr);