2 * Copyright (c) 2004 Olivier Houchard
3 * Copyright (c) 1994-1998 Mark Brinicombe.
4 * Copyright (c) 1994 Brini.
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 "opt_platform.h"
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ctype.h>
36 #include <sys/linker.h>
37 #include <sys/physmem.h>
38 #include <sys/reboot.h>
39 #include <sys/sysctl.h>
40 #if defined(LINUX_BOOT_ABI)
44 #include <machine/atags.h>
45 #include <machine/cpu.h>
46 #include <machine/machdep.h>
47 #include <machine/metadata.h>
48 #include <machine/vmparam.h> /* For KERNVIRTADDR */
51 #include <contrib/libfdt/libfdt.h>
52 #include <dev/fdt/fdt_common.h>
64 #define debugf(fmt, args...) printf(fmt, ##args)
66 #define debugf(fmt, args...)
70 static char static_kenv[4096];
75 static uint32_t board_revision;
76 /* hex representation of uint64_t */
77 static char board_serial[32];
78 static char *loader_envp;
80 #if defined(LINUX_BOOT_ABI)
81 #define LBABI_MAX_BANKS 10
82 #define CMDLINE_GUARD "FreeBSD:"
83 static uint32_t board_id;
84 static struct arm_lbabi_tag *atag_list;
85 static char linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
86 static char atags[LBABI_MAX_COMMAND_LINE * 2];
87 #endif /* defined(LINUX_BOOT_ABI) */
89 SYSCTL_NODE(_hw, OID_AUTO, board, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
91 SYSCTL_UINT(_hw_board, OID_AUTO, revision, CTLFLAG_RD,
92 &board_revision, 0, "Board revision");
93 SYSCTL_STRING(_hw_board, OID_AUTO, serial, CTLFLAG_RD,
94 board_serial, 0, "Board serial");
97 SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
98 &vfp_exists, 0, "Floating point support enabled");
101 board_set_serial(uint64_t serial)
104 snprintf(board_serial, sizeof(board_serial)-1,
109 board_set_revision(uint32_t revision)
112 board_revision = revision;
134 debugf("loader passed (static) kenv:\n");
135 if (loader_envp == NULL) {
136 debugf(" no env, null ptr\n");
139 debugf(" loader_envp = 0x%08x\n", (uint32_t)loader_envp);
141 for (cp = loader_envp; cp != NULL; cp = kenv_next(cp))
142 debugf(" %x %s\n", (uint32_t)cp, cp);
145 #if defined(LINUX_BOOT_ABI)
147 /* Convert the U-Boot command line into FreeBSD kenv and boot options. */
149 cmdline_set_env(char *cmdline, const char *guard)
153 /* Skip leading spaces. */
154 while (isspace(*cmdline))
157 /* Test and remove guard. */
158 if (guard != NULL && guard[0] != '\0') {
159 guard_len = strlen(guard);
160 if (strncasecmp(cmdline, guard, guard_len) != 0)
162 cmdline += guard_len;
165 boothowto |= boot_parse_cmdline(cmdline);
169 * Called for armv6 and newer.
171 void arm_parse_fdt_bootargs(void)
175 if (loader_envp == NULL && fdt_get_chosen_bootargs(linux_command_line,
176 LBABI_MAX_COMMAND_LINE) == 0) {
177 init_static_kenv(static_kenv, sizeof(static_kenv));
178 cmdline_set_env(linux_command_line, CMDLINE_GUARD);
184 * Called for armv[45].
187 linux_parse_boot_param(struct arm_boot_params *abp)
189 struct arm_lbabi_tag *walker;
193 vm_offset_t lastaddr;
195 struct fdt_header *dtb_ptr;
200 * Linux boot ABI: r0 = 0, r1 is the board type (!= 0) and r2
201 * is atags or dtb pointer. If all of these aren't satisfied,
202 * then punt. Unfortunately, it looks like DT enabled kernels
203 * doesn't uses board type and U-Boot delivers 0 in r1 for them.
205 if (abp->abp_r0 != 0 || abp->abp_r2 == 0)
208 /* Test if r2 point to valid DTB. */
209 dtb_ptr = (struct fdt_header *)abp->abp_r2;
210 if (fdt_check_header(dtb_ptr) == 0) {
211 dtb_size = fdt_totalsize(dtb_ptr);
212 return (fake_preload_metadata(abp, dtb_ptr, dtb_size));
216 board_id = abp->abp_r1;
217 walker = (struct arm_lbabi_tag *)abp->abp_r2;
219 if (ATAG_TAG(walker) != ATAG_CORE)
223 while (ATAG_TAG(walker) != ATAG_NONE) {
224 switch (ATAG_TAG(walker)) {
228 physmem_hardware_region(walker->u.tag_mem.start,
229 walker->u.tag_mem.size);
234 serial = walker->u.tag_sn.high;
236 serial |= walker->u.tag_sn.low;
237 board_set_serial(serial);
240 revision = walker->u.tag_rev.rev;
241 board_set_revision(revision);
244 size = ATAG_SIZE(walker) -
245 sizeof(struct arm_lbabi_header);
246 size = min(size, LBABI_MAX_COMMAND_LINE);
247 strncpy(linux_command_line, walker->u.tag_cmd.command,
249 linux_command_line[size] = '\0';
254 walker = ATAG_NEXT(walker);
257 /* Save a copy for later */
258 bcopy(atag_list, atags,
259 (char *)walker - (char *)atag_list + ATAG_SIZE(walker));
261 lastaddr = fake_preload_metadata(abp, NULL, 0);
262 init_static_kenv(static_kenv, sizeof(static_kenv));
263 cmdline_set_env(linux_command_line, CMDLINE_GUARD);
268 #if defined(FREEBSD_BOOT_LOADER)
270 freebsd_parse_boot_param(struct arm_boot_params *abp)
272 vm_offset_t lastaddr = 0;
276 vm_offset_t ksym_start;
277 vm_offset_t ksym_end;
281 * Mask metadata pointer: it is supposed to be on page boundary. If
282 * the first argument (mdp) doesn't point to a valid address the
283 * bootloader must have passed us something else than the metadata
284 * ptr, so we give up. Also give up if we cannot find metadta section
285 * the loader creates that we get all this data out of.
288 if ((mdp = (void *)(abp->abp_r0 & ~PAGE_MASK)) == NULL)
290 preload_metadata = mdp;
291 kmdp = preload_search_by_type("elf kernel");
295 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
296 loader_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
297 init_static_kenv(loader_envp, 0);
298 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
300 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
301 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
302 db_fetch_ksymtab(ksym_start, ksym_end, 0);
309 default_parse_boot_param(struct arm_boot_params *abp)
311 vm_offset_t lastaddr;
313 #if defined(LINUX_BOOT_ABI)
314 if ((lastaddr = linux_parse_boot_param(abp)) != 0)
317 #if defined(FREEBSD_BOOT_LOADER)
318 if ((lastaddr = freebsd_parse_boot_param(abp)) != 0)
321 /* Fall back to hardcoded metadata. */
322 lastaddr = fake_preload_metadata(abp, NULL, 0);
328 * Stub version of the boot parameter parsing routine. We are
329 * called early in initarm, before even VM has been initialized.
330 * This routine needs to preserve any data that the boot loader
331 * has passed in before the kernel starts to grow past the end
332 * of the BSS, traditionally the place boot-loaders put this data.
334 * Since this is called so early, things that depend on the vm system
335 * being setup (including access to some SoC's serial ports), about
336 * all that can be done in this routine is to copy the arguments.
338 * This is the default boot parameter parsing routine. Individual
339 * kernels/boards can override this weak function with one of their
340 * own. We just fake metadata...
342 __weak_reference(default_parse_boot_param, parse_boot_param);
345 * Fake up a boot descriptor table
348 fake_preload_metadata(struct arm_boot_params *abp __unused, void *dtb_ptr,
351 vm_offset_t lastaddr;
353 static uint32_t fake_preload[35];
355 lastaddr = (vm_offset_t)&end;
357 fake_preload[i++] = MODINFO_NAME;
358 fake_preload[i++] = strlen("kernel") + 1;
359 strcpy((char*)&fake_preload[i++], "kernel");
361 fake_preload[i++] = MODINFO_TYPE;
362 fake_preload[i++] = strlen("elf kernel") + 1;
363 strcpy((char*)&fake_preload[i++], "elf kernel");
365 fake_preload[i++] = MODINFO_ADDR;
366 fake_preload[i++] = sizeof(vm_offset_t);
367 fake_preload[i++] = KERNVIRTADDR;
368 fake_preload[i++] = MODINFO_SIZE;
369 fake_preload[i++] = sizeof(uint32_t);
370 fake_preload[i++] = (uint32_t)&end - KERNVIRTADDR;
371 if (dtb_ptr != NULL) {
372 /* Copy DTB to KVA space and insert it into module chain. */
373 lastaddr = roundup(lastaddr, sizeof(int));
374 fake_preload[i++] = MODINFO_METADATA | MODINFOMD_DTBP;
375 fake_preload[i++] = sizeof(uint32_t);
376 fake_preload[i++] = (uint32_t)lastaddr;
377 memmove((void *)lastaddr, dtb_ptr, dtb_size);
378 lastaddr += dtb_size;
379 lastaddr = roundup(lastaddr, sizeof(int));
381 fake_preload[i++] = 0;
383 preload_metadata = (void *)fake_preload;
385 init_static_kenv(NULL, 0);
392 arm_add_efi_map_entries(struct efi_map_header *efihdr, struct mem_region *mr,
395 struct efi_md *map, *p;
400 static const char *types[] = {
406 "RuntimeServicesCode",
407 "RuntimeServicesData",
408 "ConventionalMemory",
413 "MemoryMappedIOPortSpace",
421 * Memory map data provided by UEFI via the GetMemoryMap
424 efisz = roundup2(sizeof(struct efi_map_header), 0x10);
425 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
427 if (efihdr->descriptor_size == 0)
429 ndesc = efihdr->memory_size / efihdr->descriptor_size;
431 if (boothowto & RB_VERBOSE)
432 printf("%23s %12s %12s %8s %4s\n",
433 "Type", "Physical", "Virtual", "#Pages", "Attr");
435 for (i = 0, j = 0, p = map; i < ndesc; i++,
436 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
437 if (boothowto & RB_VERBOSE) {
438 if (p->md_type < nitems(types))
439 type = types[p->md_type];
442 printf("%23s %012llx %012llx %08llx ", type, p->md_phys,
443 p->md_virt, p->md_pages);
444 if (p->md_attr & EFI_MD_ATTR_UC)
446 if (p->md_attr & EFI_MD_ATTR_WC)
448 if (p->md_attr & EFI_MD_ATTR_WT)
450 if (p->md_attr & EFI_MD_ATTR_WB)
452 if (p->md_attr & EFI_MD_ATTR_UCE)
454 if (p->md_attr & EFI_MD_ATTR_WP)
456 if (p->md_attr & EFI_MD_ATTR_RP)
458 if (p->md_attr & EFI_MD_ATTR_XP)
460 if (p->md_attr & EFI_MD_ATTR_NV)
462 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
463 printf("MORE_RELIABLE ");
464 if (p->md_attr & EFI_MD_ATTR_RO)
466 if (p->md_attr & EFI_MD_ATTR_RT)
471 switch (p->md_type) {
472 case EFI_MD_TYPE_CODE:
473 case EFI_MD_TYPE_DATA:
474 case EFI_MD_TYPE_BS_CODE:
475 case EFI_MD_TYPE_BS_DATA:
476 case EFI_MD_TYPE_FREE:
478 * We're allowed to use any entry with these types.
486 if (j >= FDT_MEM_REGIONS)
489 mr[j].mr_start = p->md_phys;
490 mr[j].mr_size = p->md_pages * EFI_PAGE_SIZE;