xref: /freebsd/stand/uboot/main.c (revision dcc20bced51583df83e49af269ff7be5fb48dbf8)
19dc70af8SWarner Losh /*-
29dc70af8SWarner Losh  * Copyright (c) 2000 Benno Rice <benno@jeamland.net>
39dc70af8SWarner Losh  * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca>
49dc70af8SWarner Losh  * Copyright (c) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
59dc70af8SWarner Losh  * All rights reserved.
69dc70af8SWarner Losh  *
79dc70af8SWarner Losh  * Redistribution and use in source and binary forms, with or without
89dc70af8SWarner Losh  * modification, are permitted provided that the following conditions
99dc70af8SWarner Losh  * are met:
109dc70af8SWarner Losh  * 1. Redistributions of source code must retain the above copyright
119dc70af8SWarner Losh  *    notice, this list of conditions and the following disclaimer.
129dc70af8SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
139dc70af8SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
149dc70af8SWarner Losh  *    documentation and/or other materials provided with the distribution.
159dc70af8SWarner Losh  *
169dc70af8SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
179dc70af8SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dc70af8SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dc70af8SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dc70af8SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dc70af8SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dc70af8SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dc70af8SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dc70af8SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dc70af8SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dc70af8SWarner Losh  * SUCH DAMAGE.
279dc70af8SWarner Losh  */
289dc70af8SWarner Losh 
299dc70af8SWarner Losh #include <sys/param.h>
309dc70af8SWarner Losh 
319dc70af8SWarner Losh #include <stand.h>
329dc70af8SWarner Losh 
339dc70af8SWarner Losh #include "api_public.h"
349dc70af8SWarner Losh #include "bootstrap.h"
359dc70af8SWarner Losh #include "glue.h"
369dc70af8SWarner Losh #include "libuboot.h"
379dc70af8SWarner Losh 
389dc70af8SWarner Losh #ifndef nitems
399dc70af8SWarner Losh #define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
409dc70af8SWarner Losh #endif
419dc70af8SWarner Losh 
429dc70af8SWarner Losh #ifndef HEAP_SIZE
439dc70af8SWarner Losh #define	HEAP_SIZE	(2 * 1024 * 1024)
449dc70af8SWarner Losh #endif
459dc70af8SWarner Losh 
469dc70af8SWarner Losh struct uboot_devdesc currdev;
479dc70af8SWarner Losh struct arch_switch archsw;		/* MI/MD interface boundary */
489dc70af8SWarner Losh int devs_no;
499dc70af8SWarner Losh 
509dc70af8SWarner Losh uintptr_t uboot_heap_start;
519dc70af8SWarner Losh uintptr_t uboot_heap_end;
529dc70af8SWarner Losh 
539dc70af8SWarner Losh struct device_type {
549dc70af8SWarner Losh 	const char *name;
559dc70af8SWarner Losh 	int type;
569dc70af8SWarner Losh } device_types[] = {
579dc70af8SWarner Losh 	{ "disk", DEV_TYP_STOR },
589dc70af8SWarner Losh 	{ "ide",  DEV_TYP_STOR | DT_STOR_IDE },
599dc70af8SWarner Losh 	{ "mmc",  DEV_TYP_STOR | DT_STOR_MMC },
609dc70af8SWarner Losh 	{ "sata", DEV_TYP_STOR | DT_STOR_SATA },
619dc70af8SWarner Losh 	{ "scsi", DEV_TYP_STOR | DT_STOR_SCSI },
629dc70af8SWarner Losh 	{ "usb",  DEV_TYP_STOR | DT_STOR_USB },
639dc70af8SWarner Losh 	{ "net",  DEV_TYP_NET }
649dc70af8SWarner Losh };
659dc70af8SWarner Losh 
669dc70af8SWarner Losh extern char end[];
679dc70af8SWarner Losh 
689dc70af8SWarner Losh extern unsigned char _etext[];
699dc70af8SWarner Losh extern unsigned char _edata[];
709dc70af8SWarner Losh extern unsigned char __bss_start[];
719dc70af8SWarner Losh extern unsigned char __sbss_start[];
729dc70af8SWarner Losh extern unsigned char __sbss_end[];
739dc70af8SWarner Losh extern unsigned char _end[];
749dc70af8SWarner Losh 
759dc70af8SWarner Losh #ifdef LOADER_FDT_SUPPORT
769dc70af8SWarner Losh extern int command_fdt_internal(int argc, char *argv[]);
779dc70af8SWarner Losh #endif
789dc70af8SWarner Losh 
799dc70af8SWarner Losh static void
dump_sig(struct api_signature * sig)809dc70af8SWarner Losh dump_sig(struct api_signature *sig)
819dc70af8SWarner Losh {
829dc70af8SWarner Losh #ifdef DEBUG
839dc70af8SWarner Losh 	printf("signature:\n");
849dc70af8SWarner Losh 	printf("  version\t= %d\n", sig->version);
859dc70af8SWarner Losh 	printf("  checksum\t= 0x%08x\n", sig->checksum);
869dc70af8SWarner Losh 	printf("  sc entry\t= 0x%08x\n", sig->syscall);
879dc70af8SWarner Losh #endif
889dc70af8SWarner Losh }
899dc70af8SWarner Losh 
909dc70af8SWarner Losh static void
dump_addr_info(void)919dc70af8SWarner Losh dump_addr_info(void)
929dc70af8SWarner Losh {
939dc70af8SWarner Losh #ifdef DEBUG
949dc70af8SWarner Losh 	printf("\naddresses info:\n");
959dc70af8SWarner Losh 	printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
969dc70af8SWarner Losh 	printf(" _edata         = 0x%08x\n", (uint32_t)_edata);
979dc70af8SWarner Losh 	printf(" __sbss_start   = 0x%08x\n", (uint32_t)__sbss_start);
989dc70af8SWarner Losh 	printf(" __sbss_end     = 0x%08x\n", (uint32_t)__sbss_end);
999dc70af8SWarner Losh 	printf(" __sbss_start   = 0x%08x\n", (uint32_t)__bss_start);
1009dc70af8SWarner Losh 	printf(" _end           = 0x%08x\n", (uint32_t)_end);
1019dc70af8SWarner Losh 	printf(" syscall entry  = 0x%08x\n", (uint32_t)syscall_ptr);
1029dc70af8SWarner Losh #endif
1039dc70af8SWarner Losh }
1049dc70af8SWarner Losh 
1059dc70af8SWarner Losh static uint64_t
memsize(struct sys_info * si,int flags)1069dc70af8SWarner Losh memsize(struct sys_info *si, int flags)
1079dc70af8SWarner Losh {
1089dc70af8SWarner Losh 	uint64_t size;
1099dc70af8SWarner Losh 	int i;
1109dc70af8SWarner Losh 
1119dc70af8SWarner Losh 	size = 0;
1129dc70af8SWarner Losh 	for (i = 0; i < si->mr_no; i++)
1139dc70af8SWarner Losh 		if (si->mr[i].flags == flags && si->mr[i].size)
1149dc70af8SWarner Losh 			size += (si->mr[i].size);
1159dc70af8SWarner Losh 
1169dc70af8SWarner Losh 	return (size);
1179dc70af8SWarner Losh }
1189dc70af8SWarner Losh 
1199dc70af8SWarner Losh static void
meminfo(void)1209dc70af8SWarner Losh meminfo(void)
1219dc70af8SWarner Losh {
1229dc70af8SWarner Losh 	uint64_t size;
1239dc70af8SWarner Losh 	struct sys_info *si;
1249dc70af8SWarner Losh 	int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
1259dc70af8SWarner Losh 	int i;
1269dc70af8SWarner Losh 
1279dc70af8SWarner Losh 	if ((si = ub_get_sys_info()) == NULL)
1289dc70af8SWarner Losh 		panic("could not retrieve system info");
1299dc70af8SWarner Losh 
1309dc70af8SWarner Losh 	for (i = 0; i < 3; i++) {
1319dc70af8SWarner Losh 		size = memsize(si, t[i]);
1329dc70af8SWarner Losh 		if (size > 0)
1339dc70af8SWarner Losh 			printf("%s: %juMB\n", ub_mem_type(t[i]),
1349dc70af8SWarner Losh 			    (uintmax_t)(size / 1024 / 1024));
1359dc70af8SWarner Losh 	}
1369dc70af8SWarner Losh }
1379dc70af8SWarner Losh 
1389dc70af8SWarner Losh static const char *
get_device_type(const char * devstr,int * devtype)1399dc70af8SWarner Losh get_device_type(const char *devstr, int *devtype)
1409dc70af8SWarner Losh {
1419dc70af8SWarner Losh 	int i;
1429dc70af8SWarner Losh 	int namelen;
1439dc70af8SWarner Losh 	struct device_type *dt;
1449dc70af8SWarner Losh 
1459dc70af8SWarner Losh 	if (devstr) {
1469dc70af8SWarner Losh 		for (i = 0; i < nitems(device_types); i++) {
1479dc70af8SWarner Losh 			dt = &device_types[i];
1489dc70af8SWarner Losh 			namelen = strlen(dt->name);
1499dc70af8SWarner Losh 			if (strncmp(dt->name, devstr, namelen) == 0) {
1509dc70af8SWarner Losh 				*devtype = dt->type;
1519dc70af8SWarner Losh 				return (devstr + namelen);
1529dc70af8SWarner Losh 			}
1539dc70af8SWarner Losh 		}
1549dc70af8SWarner Losh 		printf("Unknown device type '%s'\n", devstr);
1559dc70af8SWarner Losh 	}
1569dc70af8SWarner Losh 
1579dc70af8SWarner Losh 	*devtype = DEV_TYP_NONE;
1589dc70af8SWarner Losh 	return (NULL);
1599dc70af8SWarner Losh }
1609dc70af8SWarner Losh 
1619dc70af8SWarner Losh static const char *
device_typename(int type)1629dc70af8SWarner Losh device_typename(int type)
1639dc70af8SWarner Losh {
1649dc70af8SWarner Losh 	int i;
1659dc70af8SWarner Losh 
1669dc70af8SWarner Losh 	for (i = 0; i < nitems(device_types); i++)
1679dc70af8SWarner Losh 		if (device_types[i].type == type)
1689dc70af8SWarner Losh 			return (device_types[i].name);
1699dc70af8SWarner Losh 
1709dc70af8SWarner Losh 	return ("<unknown>");
1719dc70af8SWarner Losh }
1729dc70af8SWarner Losh 
1739dc70af8SWarner Losh /*
1749dc70af8SWarner Losh  * Parse a device string into type, unit, slice and partition numbers. A
1759dc70af8SWarner Losh  * returned value of -1 for type indicates a search should be done for the
1769dc70af8SWarner Losh  * first loadable device, otherwise a returned value of -1 for unit
1779dc70af8SWarner Losh  * indicates a search should be done for the first loadable device of the
1789dc70af8SWarner Losh  * given type.
1799dc70af8SWarner Losh  *
1809dc70af8SWarner Losh  * The returned values for slice and partition are interpreted by
1819dc70af8SWarner Losh  * disk_open().
1829dc70af8SWarner Losh  *
1839dc70af8SWarner Losh  * The device string can be a standard loader(8) disk specifier:
1849dc70af8SWarner Losh  *
1859dc70af8SWarner Losh  * disk<unit>s<slice>              disk0s1
1869dc70af8SWarner Losh  * disk<unit>s<slice><partition>   disk1s2a
1879dc70af8SWarner Losh  * disk<unit>p<partition>          disk0p4
1889dc70af8SWarner Losh  *
1899dc70af8SWarner Losh  * or one of the following formats:
1909dc70af8SWarner Losh  *
1919dc70af8SWarner Losh  * Valid device strings:                     For device types:
1929dc70af8SWarner Losh  *
1939dc70af8SWarner Losh  * <type_name>                               DEV_TYP_STOR, DEV_TYP_NET
1949dc70af8SWarner Losh  * <type_name><unit>                         DEV_TYP_STOR, DEV_TYP_NET
1959dc70af8SWarner Losh  * <type_name><unit>:                        DEV_TYP_STOR, DEV_TYP_NET
1969dc70af8SWarner Losh  * <type_name><unit>:<slice>                 DEV_TYP_STOR
1979dc70af8SWarner Losh  * <type_name><unit>:<slice>.                DEV_TYP_STOR
1989dc70af8SWarner Losh  * <type_name><unit>:<slice>.<partition>     DEV_TYP_STOR
1999dc70af8SWarner Losh  *
2009dc70af8SWarner Losh  * For valid type names, see the device_types array, above.
2019dc70af8SWarner Losh  *
2029dc70af8SWarner Losh  * Slice numbers are 1-based.  0 is a wildcard.
2039dc70af8SWarner Losh  */
2049dc70af8SWarner Losh static void
get_load_device(int * type,int * unit,int * slice,int * partition)2059dc70af8SWarner Losh get_load_device(int *type, int *unit, int *slice, int *partition)
2069dc70af8SWarner Losh {
20717276525SWarner Losh 	struct disk_devdesc *dev;
2089dc70af8SWarner Losh 	char *devstr;
2099dc70af8SWarner Losh 	const char *p;
2109dc70af8SWarner Losh 	char *endp;
2119dc70af8SWarner Losh 
2129dc70af8SWarner Losh 	*type = DEV_TYP_NONE;
2139dc70af8SWarner Losh 	*unit = -1;
2149dc70af8SWarner Losh 	*slice = D_SLICEWILD;
2159dc70af8SWarner Losh 	*partition = D_PARTWILD;
2169dc70af8SWarner Losh 
2179dc70af8SWarner Losh 	devstr = ub_env_get("loaderdev");
2189dc70af8SWarner Losh 	if (devstr == NULL) {
2199dc70af8SWarner Losh 		printf("U-Boot env: loaderdev not set, will probe all devices.\n");
2209dc70af8SWarner Losh 		return;
2219dc70af8SWarner Losh 	}
2229dc70af8SWarner Losh 	printf("U-Boot env: loaderdev='%s'\n", devstr);
2239dc70af8SWarner Losh 
2249dc70af8SWarner Losh 	p = get_device_type(devstr, type);
2259dc70af8SWarner Losh 
2269dc70af8SWarner Losh 	/*
2279dc70af8SWarner Losh 	 * If type is DEV_TYP_STOR we have a disk-like device.  If the remainder
2289dc70af8SWarner Losh 	 * of the string contains spaces, dots, or a colon in any location other
2299dc70af8SWarner Losh 	 * than the last char, it's legacy format.  Otherwise it might be
2309dc70af8SWarner Losh 	 * standard loader(8) format (e.g., disk0s2a or mmc1p12), so try to
2319dc70af8SWarner Losh 	 * parse the remainder of the string as such, and if it works, return
2329dc70af8SWarner Losh 	 * those results. Otherwise we'll fall through to the code that parses
2339dc70af8SWarner Losh 	 * the legacy format.
23433bbe5ddSWarner Losh 	 *
23533bbe5ddSWarner Losh 	 * disk_parsedev now assumes that it points to the start of the device
23633bbe5ddSWarner Losh 	 * name, but since it doesn't know about uboot's usage, just subtract 4
23733bbe5ddSWarner Losh 	 * since it always adds 4. This is the least-bad solution since it makes
23833bbe5ddSWarner Losh 	 * all the other loader code easier (might be better to create a fake
23933bbe5ddSWarner Losh 	 * 'disk...' string, but that's more work than uboot is worth).
2409dc70af8SWarner Losh 	 */
2419dc70af8SWarner Losh 	if (*type & DEV_TYP_STOR) {
2429dc70af8SWarner Losh 		size_t len = strlen(p);
2439dc70af8SWarner Losh 		if (strcspn(p, " .") == len && strcspn(p, ":") >= len - 1 &&
24433bbe5ddSWarner Losh 		    disk_parsedev((struct devdesc **)&dev, p - 4, NULL) == 0) { /* Hack */
24517276525SWarner Losh 			*unit = dev->dd.d_unit;
24617276525SWarner Losh 			*slice = dev->d_slice;
24717276525SWarner Losh 			*partition = dev->d_partition;
24817276525SWarner Losh 			free(dev);
2499dc70af8SWarner Losh 			return;
2509dc70af8SWarner Losh 		}
2519dc70af8SWarner Losh 	}
2529dc70af8SWarner Losh 
2539dc70af8SWarner Losh 	/* Ignore optional spaces after the device name. */
2549dc70af8SWarner Losh 	while (*p == ' ')
2559dc70af8SWarner Losh 		p++;
2569dc70af8SWarner Losh 
2579dc70af8SWarner Losh 	/* Unknown device name, or a known name without unit number.  */
2589dc70af8SWarner Losh 	if ((*type == DEV_TYP_NONE) || (*p == '\0')) {
2599dc70af8SWarner Losh 		return;
2609dc70af8SWarner Losh 	}
2619dc70af8SWarner Losh 
2629dc70af8SWarner Losh 	/* Malformed unit number. */
2639dc70af8SWarner Losh 	if (!isdigit(*p)) {
2649dc70af8SWarner Losh 		*type = DEV_TYP_NONE;
2659dc70af8SWarner Losh 		return;
2669dc70af8SWarner Losh 	}
2679dc70af8SWarner Losh 
2689dc70af8SWarner Losh 	/* Guaranteed to extract a number from the string, as *p is a digit. */
2699dc70af8SWarner Losh 	*unit = strtol(p, &endp, 10);
2709dc70af8SWarner Losh 	p = endp;
2719dc70af8SWarner Losh 
2729dc70af8SWarner Losh 	/* Known device name with unit number and nothing else. */
2739dc70af8SWarner Losh 	if (*p == '\0') {
2749dc70af8SWarner Losh 		return;
2759dc70af8SWarner Losh 	}
2769dc70af8SWarner Losh 
2779dc70af8SWarner Losh 	/* Device string is malformed beyond unit number. */
2789dc70af8SWarner Losh 	if (*p != ':') {
2799dc70af8SWarner Losh 		*type = DEV_TYP_NONE;
2809dc70af8SWarner Losh 		*unit = -1;
2819dc70af8SWarner Losh 		return;
2829dc70af8SWarner Losh 	}
2839dc70af8SWarner Losh 
2849dc70af8SWarner Losh 	p++;
2859dc70af8SWarner Losh 
2869dc70af8SWarner Losh 	/* No slice and partition specification. */
2879dc70af8SWarner Losh 	if ('\0' == *p )
2889dc70af8SWarner Losh 		return;
2899dc70af8SWarner Losh 
2909dc70af8SWarner Losh 	/* Only DEV_TYP_STOR devices can have a slice specification. */
2919dc70af8SWarner Losh 	if (!(*type & DEV_TYP_STOR)) {
2929dc70af8SWarner Losh 		*type = DEV_TYP_NONE;
2939dc70af8SWarner Losh 		*unit = -1;
2949dc70af8SWarner Losh 		return;
2959dc70af8SWarner Losh 	}
2969dc70af8SWarner Losh 
2979dc70af8SWarner Losh 	*slice = strtoul(p, &endp, 10);
2989dc70af8SWarner Losh 
2999dc70af8SWarner Losh 	/* Malformed slice number. */
3009dc70af8SWarner Losh 	if (p == endp) {
3019dc70af8SWarner Losh 		*type = DEV_TYP_NONE;
3029dc70af8SWarner Losh 		*unit = -1;
3039dc70af8SWarner Losh 		*slice = D_SLICEWILD;
3049dc70af8SWarner Losh 		return;
3059dc70af8SWarner Losh 	}
3069dc70af8SWarner Losh 
3079dc70af8SWarner Losh 	p = endp;
3089dc70af8SWarner Losh 
3099dc70af8SWarner Losh 	/* No partition specification. */
3109dc70af8SWarner Losh 	if (*p == '\0')
3119dc70af8SWarner Losh 		return;
3129dc70af8SWarner Losh 
3139dc70af8SWarner Losh 	/* Device string is malformed beyond slice number. */
3149dc70af8SWarner Losh 	if (*p != '.') {
3159dc70af8SWarner Losh 		*type = DEV_TYP_NONE;
3169dc70af8SWarner Losh 		*unit = -1;
3179dc70af8SWarner Losh 		*slice = D_SLICEWILD;
3189dc70af8SWarner Losh 		return;
3199dc70af8SWarner Losh 	}
3209dc70af8SWarner Losh 
3219dc70af8SWarner Losh 	p++;
3229dc70af8SWarner Losh 
3239dc70af8SWarner Losh 	/* No partition specification. */
3249dc70af8SWarner Losh 	if (*p == '\0')
3259dc70af8SWarner Losh 		return;
3269dc70af8SWarner Losh 
3279dc70af8SWarner Losh 	*partition = strtol(p, &endp, 10);
3289dc70af8SWarner Losh 	p = endp;
3299dc70af8SWarner Losh 
3309dc70af8SWarner Losh 	/*  Full, valid device string. */
3319dc70af8SWarner Losh 	if (*endp == '\0')
3329dc70af8SWarner Losh 		return;
3339dc70af8SWarner Losh 
3349dc70af8SWarner Losh 	/* Junk beyond partition number. */
3359dc70af8SWarner Losh 	*type = DEV_TYP_NONE;
3369dc70af8SWarner Losh 	*unit = -1;
3379dc70af8SWarner Losh 	*slice = D_SLICEWILD;
3389dc70af8SWarner Losh 	*partition = D_PARTWILD;
3399dc70af8SWarner Losh }
3409dc70af8SWarner Losh 
3419dc70af8SWarner Losh static void
print_disk_probe_info(void)342*dcc20bceSWarner Losh print_disk_probe_info(void)
3439dc70af8SWarner Losh {
3449dc70af8SWarner Losh 	char slice[32];
3459dc70af8SWarner Losh 	char partition[32];
3469dc70af8SWarner Losh 
3479dc70af8SWarner Losh 	if (currdev.d_disk.d_slice == D_SLICENONE)
3489dc70af8SWarner Losh 		strlcpy(slice, "<none>", sizeof(slice));
3499dc70af8SWarner Losh 	else if (currdev.d_disk.d_slice == D_SLICEWILD)
3509dc70af8SWarner Losh 		strlcpy(slice, "<auto>", sizeof(slice));
3519dc70af8SWarner Losh 	else
3529dc70af8SWarner Losh 		snprintf(slice, sizeof(slice), "%d", currdev.d_disk.d_slice);
3539dc70af8SWarner Losh 
3549dc70af8SWarner Losh 	if (currdev.d_disk.d_partition == D_PARTNONE)
3559dc70af8SWarner Losh 		strlcpy(partition, "<none>", sizeof(partition));
3569dc70af8SWarner Losh 	else if (currdev.d_disk.d_partition == D_PARTWILD)
3579dc70af8SWarner Losh 		strlcpy(partition, "<auto>", sizeof(partition));
3589dc70af8SWarner Losh 	else
3599dc70af8SWarner Losh 		snprintf(partition, sizeof(partition), "%d",
3609dc70af8SWarner Losh 		    currdev.d_disk.d_partition);
3619dc70af8SWarner Losh 
3629dc70af8SWarner Losh 	printf("  Checking unit=%d slice=%s partition=%s...",
3639dc70af8SWarner Losh 	    currdev.dd.d_unit, slice, partition);
3649dc70af8SWarner Losh 
3659dc70af8SWarner Losh }
3669dc70af8SWarner Losh 
3679dc70af8SWarner Losh static int
probe_disks(int devidx,int load_type,int load_unit,int load_slice,int load_partition)3689dc70af8SWarner Losh probe_disks(int devidx, int load_type, int load_unit, int load_slice,
3699dc70af8SWarner Losh     int load_partition)
3709dc70af8SWarner Losh {
3719dc70af8SWarner Losh 	int open_result, unit;
3729dc70af8SWarner Losh 	struct open_file f;
3739dc70af8SWarner Losh 
3749dc70af8SWarner Losh 	currdev.d_disk.d_slice = load_slice;
3759dc70af8SWarner Losh 	currdev.d_disk.d_partition = load_partition;
3769dc70af8SWarner Losh 
3779dc70af8SWarner Losh 	f.f_devdata = &currdev;
3789dc70af8SWarner Losh 	open_result = -1;
3799dc70af8SWarner Losh 
3809dc70af8SWarner Losh 	if (load_type == -1) {
3819dc70af8SWarner Losh 		printf("  Probing all disk devices...\n");
3829dc70af8SWarner Losh 		/* Try each disk in succession until one works.  */
3839dc70af8SWarner Losh 		for (currdev.dd.d_unit = 0; currdev.dd.d_unit < UB_MAX_DEV;
3849dc70af8SWarner Losh 		     currdev.dd.d_unit++) {
3859dc70af8SWarner Losh 			print_disk_probe_info();
3869dc70af8SWarner Losh 			open_result = devsw[devidx]->dv_open(&f, &currdev);
3879dc70af8SWarner Losh 			if (open_result == 0) {
3889dc70af8SWarner Losh 				printf(" good.\n");
3899dc70af8SWarner Losh 				return (0);
3909dc70af8SWarner Losh 			}
3919dc70af8SWarner Losh 			printf("\n");
3929dc70af8SWarner Losh 		}
3939dc70af8SWarner Losh 		return (-1);
3949dc70af8SWarner Losh 	}
3959dc70af8SWarner Losh 
3969dc70af8SWarner Losh 	if (load_unit == -1) {
3979dc70af8SWarner Losh 		printf("  Probing all %s devices...\n", device_typename(load_type));
3989dc70af8SWarner Losh 		/* Try each disk of given type in succession until one works. */
3999dc70af8SWarner Losh 		for (unit = 0; unit < UB_MAX_DEV; unit++) {
4009dc70af8SWarner Losh 			currdev.dd.d_unit = uboot_diskgetunit(load_type, unit);
4019dc70af8SWarner Losh 			if (currdev.dd.d_unit == -1)
4029dc70af8SWarner Losh 				break;
4039dc70af8SWarner Losh 			print_disk_probe_info();
4049dc70af8SWarner Losh 			open_result = devsw[devidx]->dv_open(&f, &currdev);
4059dc70af8SWarner Losh 			if (open_result == 0) {
4069dc70af8SWarner Losh 				printf(" good.\n");
4079dc70af8SWarner Losh 				return (0);
4089dc70af8SWarner Losh 			}
4099dc70af8SWarner Losh 			printf("\n");
4109dc70af8SWarner Losh 		}
4119dc70af8SWarner Losh 		return (-1);
4129dc70af8SWarner Losh 	}
4139dc70af8SWarner Losh 
4149dc70af8SWarner Losh 	if ((currdev.dd.d_unit = uboot_diskgetunit(load_type, load_unit)) != -1) {
4159dc70af8SWarner Losh 		print_disk_probe_info();
4169dc70af8SWarner Losh 		open_result = devsw[devidx]->dv_open(&f,&currdev);
4179dc70af8SWarner Losh 		if (open_result == 0) {
4189dc70af8SWarner Losh 			printf(" good.\n");
4199dc70af8SWarner Losh 			return (0);
4209dc70af8SWarner Losh 		}
4219dc70af8SWarner Losh 		printf("\n");
4229dc70af8SWarner Losh 	}
4239dc70af8SWarner Losh 
4249dc70af8SWarner Losh 	printf("  Requested disk type/unit/slice/partition not found\n");
4259dc70af8SWarner Losh 	return (-1);
4269dc70af8SWarner Losh }
4279dc70af8SWarner Losh 
4289dc70af8SWarner Losh int
main(int argc,char ** argv)4299dc70af8SWarner Losh main(int argc, char **argv)
4309dc70af8SWarner Losh {
4319dc70af8SWarner Losh 	struct api_signature *sig = NULL;
4329dc70af8SWarner Losh 	int load_type, load_unit, load_slice, load_partition;
4339dc70af8SWarner Losh 	int i;
4349dc70af8SWarner Losh 	const char *ldev;
4359dc70af8SWarner Losh 
4369dc70af8SWarner Losh 	/*
4379dc70af8SWarner Losh 	 * We first check if a command line argument was passed to us containing
4389dc70af8SWarner Losh 	 * API's signature address. If it wasn't then we try to search for the
4399dc70af8SWarner Losh 	 * API signature via the usual hinted address.
4409dc70af8SWarner Losh 	 * If we can't find the magic signature and related info, exit with a
4419dc70af8SWarner Losh 	 * unique error code that U-Boot reports as "## Application terminated,
4429dc70af8SWarner Losh 	 * rc = 0xnnbadab1". Hopefully 'badab1' looks enough like "bad api" to
4439dc70af8SWarner Losh 	 * provide a clue. It's better than 0xffffffff anyway.
4449dc70af8SWarner Losh 	 */
4459dc70af8SWarner Losh 	if (!api_parse_cmdline_sig(argc, argv, &sig) && !api_search_sig(&sig))
4469dc70af8SWarner Losh 		return (0x01badab1);
4479dc70af8SWarner Losh 
4489dc70af8SWarner Losh 	syscall_ptr = sig->syscall;
4499dc70af8SWarner Losh 	if (syscall_ptr == NULL)
4509dc70af8SWarner Losh 		return (0x02badab1);
4519dc70af8SWarner Losh 
4529dc70af8SWarner Losh 	if (sig->version > API_SIG_VERSION)
4539dc70af8SWarner Losh 		return (0x03badab1);
4549dc70af8SWarner Losh 
4559dc70af8SWarner Losh         /* Clear BSS sections */
4569dc70af8SWarner Losh 	bzero(__sbss_start, __sbss_end - __sbss_start);
4579dc70af8SWarner Losh 	bzero(__bss_start, _end - __bss_start);
4589dc70af8SWarner Losh 
4599dc70af8SWarner Losh 	/*
4609dc70af8SWarner Losh 	 * Initialise the heap as early as possible.  Once this is done,
4619dc70af8SWarner Losh 	 * alloc() is usable.  We are using the stack u-boot set up near the top
4629dc70af8SWarner Losh 	 * of physical ram; hopefully there is sufficient space between the end
4639dc70af8SWarner Losh 	 * of our bss and the bottom of the u-boot stack to avoid overlap.
4649dc70af8SWarner Losh 	 */
4659dc70af8SWarner Losh 	uboot_heap_start = round_page((uintptr_t)end);
4669dc70af8SWarner Losh 	uboot_heap_end   = uboot_heap_start + HEAP_SIZE;
4679dc70af8SWarner Losh 	setheap((void *)uboot_heap_start, (void *)uboot_heap_end);
4689dc70af8SWarner Losh 
4699dc70af8SWarner Losh 	/*
4709dc70af8SWarner Losh 	 * Set up console.
4719dc70af8SWarner Losh 	 */
4729dc70af8SWarner Losh 	cons_probe();
4739dc70af8SWarner Losh 	printf("Compatible U-Boot API signature found @%p\n", sig);
4749dc70af8SWarner Losh 
4759dc70af8SWarner Losh 	printf("\n%s", bootprog_info);
4769dc70af8SWarner Losh 	printf("\n");
4779dc70af8SWarner Losh 
4789dc70af8SWarner Losh 	dump_sig(sig);
4799dc70af8SWarner Losh 	dump_addr_info();
4809dc70af8SWarner Losh 
4819dc70af8SWarner Losh 	meminfo();
4829dc70af8SWarner Losh 
4831323f0aaSAlbert Jakiela 	archsw.arch_loadaddr = uboot_loadaddr;
4841323f0aaSAlbert Jakiela 	archsw.arch_getdev = uboot_getdev;
4851323f0aaSAlbert Jakiela 	archsw.arch_copyin = uboot_copyin;
4861323f0aaSAlbert Jakiela 	archsw.arch_copyout = uboot_copyout;
4871323f0aaSAlbert Jakiela 	archsw.arch_readin = uboot_readin;
4881323f0aaSAlbert Jakiela 	archsw.arch_autoload = uboot_autoload;
4891323f0aaSAlbert Jakiela 
4909dc70af8SWarner Losh 	/* Set up currdev variable to have hooks in place. */
4919dc70af8SWarner Losh 	env_setenv("currdev", EV_VOLATILE, "", uboot_setcurrdev, env_nounset);
4929dc70af8SWarner Losh 
4939dc70af8SWarner Losh 	/*
4949dc70af8SWarner Losh 	 * Enumerate U-Boot devices
4959dc70af8SWarner Losh 	 */
4969dc70af8SWarner Losh 	if ((devs_no = ub_dev_enum()) == 0) {
4979dc70af8SWarner Losh 		printf("no U-Boot devices found");
4989dc70af8SWarner Losh 		goto do_interact;
4999dc70af8SWarner Losh 	}
5009dc70af8SWarner Losh 	printf("Number of U-Boot devices: %d\n", devs_no);
5019dc70af8SWarner Losh 
5029dc70af8SWarner Losh 	get_load_device(&load_type, &load_unit, &load_slice, &load_partition);
5039dc70af8SWarner Losh 
5049dc70af8SWarner Losh 	/*
5059dc70af8SWarner Losh 	 * March through the device switch probing for things.
5069dc70af8SWarner Losh 	 */
5079dc70af8SWarner Losh 	for (i = 0; devsw[i] != NULL; i++) {
5089dc70af8SWarner Losh 
5099dc70af8SWarner Losh 		if (devsw[i]->dv_init == NULL)
5109dc70af8SWarner Losh 			continue;
5119dc70af8SWarner Losh 		if ((devsw[i]->dv_init)() != 0)
5129dc70af8SWarner Losh 			continue;
5139dc70af8SWarner Losh 
5149dc70af8SWarner Losh 		printf("Found U-Boot device: %s\n", devsw[i]->dv_name);
5159dc70af8SWarner Losh 
5169dc70af8SWarner Losh 		currdev.dd.d_dev = devsw[i];
5179dc70af8SWarner Losh 		currdev.dd.d_unit = 0;
5189dc70af8SWarner Losh 
5199dc70af8SWarner Losh 		if ((load_type == DEV_TYP_NONE || (load_type & DEV_TYP_STOR)) &&
5209dc70af8SWarner Losh 		    strcmp(devsw[i]->dv_name, "disk") == 0) {
5219dc70af8SWarner Losh 			if (probe_disks(i, load_type, load_unit, load_slice,
5229dc70af8SWarner Losh 			    load_partition) == 0)
5239dc70af8SWarner Losh 				break;
5249dc70af8SWarner Losh 		}
5259dc70af8SWarner Losh 
5269dc70af8SWarner Losh 		if ((load_type == DEV_TYP_NONE || (load_type & DEV_TYP_NET)) &&
5279dc70af8SWarner Losh 		    strcmp(devsw[i]->dv_name, "net") == 0)
5289dc70af8SWarner Losh 			break;
5299dc70af8SWarner Losh 	}
5309dc70af8SWarner Losh 
5319dc70af8SWarner Losh 	/*
5329dc70af8SWarner Losh 	 * If we couldn't find a boot device, return an error to u-boot.
5339dc70af8SWarner Losh 	 * U-boot may be running a boot script that can try something different
5349dc70af8SWarner Losh 	 * so returning an error is better than forcing a reboot.
5359dc70af8SWarner Losh 	 */
5369dc70af8SWarner Losh 	if (devsw[i] == NULL) {
5379dc70af8SWarner Losh 		printf("No boot device found!\n");
5389dc70af8SWarner Losh 		return (0xbadef1ce);
5399dc70af8SWarner Losh 	}
5409dc70af8SWarner Losh 
541add8154eSWarner Losh 	ldev = devformat(&currdev.dd);
5429dc70af8SWarner Losh 	env_setenv("currdev", EV_VOLATILE, ldev, uboot_setcurrdev, env_nounset);
5439dc70af8SWarner Losh 	env_setenv("loaddev", EV_VOLATILE, ldev, env_noset, env_nounset);
5449dc70af8SWarner Losh 	printf("Booting from %s\n", ldev);
5459dc70af8SWarner Losh 
5469dc70af8SWarner Losh do_interact:
5479dc70af8SWarner Losh 	setenv("LINES", "24", 1);		/* optional */
5489dc70af8SWarner Losh 	setenv("prompt", "loader>", 1);
5499dc70af8SWarner Losh #ifdef __powerpc__
5509dc70af8SWarner Losh 	setenv("usefdt", "1", 1);
5519dc70af8SWarner Losh #endif
5529dc70af8SWarner Losh 
5539dc70af8SWarner Losh 	interact();				/* doesn't return */
5549dc70af8SWarner Losh 
5559dc70af8SWarner Losh 	return (0);
5569dc70af8SWarner Losh }
5579dc70af8SWarner Losh 
5589dc70af8SWarner Losh 
5599dc70af8SWarner Losh COMMAND_SET(heap, "heap", "show heap usage", command_heap);
5609dc70af8SWarner Losh static int
command_heap(int argc,char * argv[])5619dc70af8SWarner Losh command_heap(int argc, char *argv[])
5629dc70af8SWarner Losh {
5639dc70af8SWarner Losh 
5649dc70af8SWarner Losh 	printf("heap base at %p, top at %p, used %td\n", end, sbrk(0),
5659dc70af8SWarner Losh 	    sbrk(0) - end);
5669dc70af8SWarner Losh 
5679dc70af8SWarner Losh 	return (CMD_OK);
5689dc70af8SWarner Losh }
5699dc70af8SWarner Losh 
5709dc70af8SWarner Losh COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
5719dc70af8SWarner Losh static int
command_reboot(int argc,char * argv[])5729dc70af8SWarner Losh command_reboot(int argc, char *argv[])
5739dc70af8SWarner Losh {
5749dc70af8SWarner Losh 
5759dc70af8SWarner Losh 	printf("Resetting...\n");
5769dc70af8SWarner Losh 	ub_reset();
5779dc70af8SWarner Losh 
5789dc70af8SWarner Losh 	printf("Reset failed!\n");
5799dc70af8SWarner Losh 	while (1);
5809dc70af8SWarner Losh 	__unreachable();
5819dc70af8SWarner Losh }
5829dc70af8SWarner Losh 
5839dc70af8SWarner Losh COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
5849dc70af8SWarner Losh static int
command_devinfo(int argc,char * argv[])5859dc70af8SWarner Losh command_devinfo(int argc, char *argv[])
5869dc70af8SWarner Losh {
5879dc70af8SWarner Losh 	int i;
5889dc70af8SWarner Losh 
5899dc70af8SWarner Losh 	if ((devs_no = ub_dev_enum()) == 0) {
5909dc70af8SWarner Losh 		command_errmsg = "no U-Boot devices found!?";
5919dc70af8SWarner Losh 		return (CMD_ERROR);
5929dc70af8SWarner Losh 	}
5939dc70af8SWarner Losh 
5949dc70af8SWarner Losh 	printf("U-Boot devices:\n");
5959dc70af8SWarner Losh 	for (i = 0; i < devs_no; i++) {
5969dc70af8SWarner Losh 		ub_dump_di(i);
5979dc70af8SWarner Losh 		printf("\n");
5989dc70af8SWarner Losh 	}
5999dc70af8SWarner Losh 	return (CMD_OK);
6009dc70af8SWarner Losh }
6019dc70af8SWarner Losh 
6029dc70af8SWarner Losh COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
6039dc70af8SWarner Losh static int
command_sysinfo(int argc,char * argv[])6049dc70af8SWarner Losh command_sysinfo(int argc, char *argv[])
6059dc70af8SWarner Losh {
6069dc70af8SWarner Losh 	struct sys_info *si;
6079dc70af8SWarner Losh 
6089dc70af8SWarner Losh 	if ((si = ub_get_sys_info()) == NULL) {
6099dc70af8SWarner Losh 		command_errmsg = "could not retrieve U-Boot sys info!?";
6109dc70af8SWarner Losh 		return (CMD_ERROR);
6119dc70af8SWarner Losh 	}
6129dc70af8SWarner Losh 
6139dc70af8SWarner Losh 	printf("U-Boot system info:\n");
6149dc70af8SWarner Losh 	ub_dump_si(si);
6159dc70af8SWarner Losh 	return (CMD_OK);
6169dc70af8SWarner Losh }
6179dc70af8SWarner Losh 
6189dc70af8SWarner Losh enum ubenv_action {
6199dc70af8SWarner Losh 	UBENV_UNKNOWN,
6209dc70af8SWarner Losh 	UBENV_SHOW,
6219dc70af8SWarner Losh 	UBENV_IMPORT
6229dc70af8SWarner Losh };
6239dc70af8SWarner Losh 
6249dc70af8SWarner Losh static void
handle_uboot_env_var(enum ubenv_action action,const char * var)6259dc70af8SWarner Losh handle_uboot_env_var(enum ubenv_action action, const char * var)
6269dc70af8SWarner Losh {
6279dc70af8SWarner Losh 	char ldvar[128];
6289dc70af8SWarner Losh 	const char *val;
6299dc70af8SWarner Losh 	char *wrk;
6309dc70af8SWarner Losh 	int len;
6319dc70af8SWarner Losh 
6329dc70af8SWarner Losh 	/*
6339dc70af8SWarner Losh 	 * On an import with the variable name formatted as ldname=ubname,
6349dc70af8SWarner Losh 	 * import the uboot variable ubname into the loader variable ldname,
6359dc70af8SWarner Losh 	 * otherwise the historical behavior is to import to uboot.ubname.
6369dc70af8SWarner Losh 	 */
6379dc70af8SWarner Losh 	if (action == UBENV_IMPORT) {
6389dc70af8SWarner Losh 		len = strcspn(var, "=");
6399dc70af8SWarner Losh 		if (len == 0) {
6409dc70af8SWarner Losh 			printf("name cannot start with '=': '%s'\n", var);
6419dc70af8SWarner Losh 			return;
6429dc70af8SWarner Losh 		}
6439dc70af8SWarner Losh 		if (var[len] == 0) {
6449dc70af8SWarner Losh 			strcpy(ldvar, "uboot.");
6459dc70af8SWarner Losh 			strncat(ldvar, var, sizeof(ldvar) - 7);
6469dc70af8SWarner Losh 		} else {
6479dc70af8SWarner Losh 			len = MIN(len, sizeof(ldvar) - 1);
6489dc70af8SWarner Losh 			strncpy(ldvar, var, len);
6499dc70af8SWarner Losh 			ldvar[len] = 0;
6509dc70af8SWarner Losh 			var = &var[len + 1];
6519dc70af8SWarner Losh 		}
6529dc70af8SWarner Losh 	}
6539dc70af8SWarner Losh 
6549dc70af8SWarner Losh 	/*
6559dc70af8SWarner Losh 	 * If the user prepended "uboot." (which is how they usually see these
6569dc70af8SWarner Losh 	 * names) strip it off as a convenience.
6579dc70af8SWarner Losh 	 */
6589dc70af8SWarner Losh 	if (strncmp(var, "uboot.", 6) == 0) {
6599dc70af8SWarner Losh 		var = &var[6];
6609dc70af8SWarner Losh 	}
6619dc70af8SWarner Losh 
6629dc70af8SWarner Losh 	/* If there is no variable name left, punt. */
6639dc70af8SWarner Losh 	if (var[0] == 0) {
6649dc70af8SWarner Losh 		printf("empty variable name\n");
6659dc70af8SWarner Losh 		return;
6669dc70af8SWarner Losh 	}
6679dc70af8SWarner Losh 
6689dc70af8SWarner Losh 	val = ub_env_get(var);
6699dc70af8SWarner Losh 	if (action == UBENV_SHOW) {
6709dc70af8SWarner Losh 		if (val == NULL)
6719dc70af8SWarner Losh 			printf("uboot.%s is not set\n", var);
6729dc70af8SWarner Losh 		else
6739dc70af8SWarner Losh 			printf("uboot.%s=%s\n", var, val);
6749dc70af8SWarner Losh 	} else if (action == UBENV_IMPORT) {
6759dc70af8SWarner Losh 		if (val != NULL) {
6769dc70af8SWarner Losh 			setenv(ldvar, val, 1);
6779dc70af8SWarner Losh 		}
6789dc70af8SWarner Losh 	}
6799dc70af8SWarner Losh }
6809dc70af8SWarner Losh 
6819dc70af8SWarner Losh static int
command_ubenv(int argc,char * argv[])6829dc70af8SWarner Losh command_ubenv(int argc, char *argv[])
6839dc70af8SWarner Losh {
6849dc70af8SWarner Losh 	enum ubenv_action action;
6859dc70af8SWarner Losh 	const char *var;
6869dc70af8SWarner Losh 	int i;
6879dc70af8SWarner Losh 
6889dc70af8SWarner Losh 	action = UBENV_UNKNOWN;
6899dc70af8SWarner Losh 	if (argc > 1) {
6909dc70af8SWarner Losh 		if (strcasecmp(argv[1], "import") == 0)
6919dc70af8SWarner Losh 			action = UBENV_IMPORT;
6929dc70af8SWarner Losh 		else if (strcasecmp(argv[1], "show") == 0)
6939dc70af8SWarner Losh 			action = UBENV_SHOW;
6949dc70af8SWarner Losh 	}
6959dc70af8SWarner Losh 	if (action == UBENV_UNKNOWN) {
6969dc70af8SWarner Losh 		command_errmsg = "usage: 'ubenv <import|show> [var ...]";
6979dc70af8SWarner Losh 		return (CMD_ERROR);
6989dc70af8SWarner Losh 	}
6999dc70af8SWarner Losh 
7009dc70af8SWarner Losh 	if (argc > 2) {
7019dc70af8SWarner Losh 		for (i = 2; i < argc; i++)
7029dc70af8SWarner Losh 			handle_uboot_env_var(action, argv[i]);
7039dc70af8SWarner Losh 	} else {
7049dc70af8SWarner Losh 		var = NULL;
7059dc70af8SWarner Losh 		for (;;) {
7069dc70af8SWarner Losh 			if ((var = ub_env_enum(var)) == NULL)
7079dc70af8SWarner Losh 				break;
7089dc70af8SWarner Losh 			handle_uboot_env_var(action, var);
7099dc70af8SWarner Losh 		}
7109dc70af8SWarner Losh 	}
7119dc70af8SWarner Losh 
7129dc70af8SWarner Losh 	return (CMD_OK);
7139dc70af8SWarner Losh }
7149dc70af8SWarner Losh COMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv);
7159dc70af8SWarner Losh 
7169dc70af8SWarner Losh #ifdef LOADER_FDT_SUPPORT
7179dc70af8SWarner Losh /*
7189dc70af8SWarner Losh  * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
7199dc70af8SWarner Losh  * and declaring it as extern is in contradiction with COMMAND_SET() macro
7209dc70af8SWarner Losh  * (which uses static pointer), we're defining wrapper function, which
7219dc70af8SWarner Losh  * calls the proper fdt handling routine.
7229dc70af8SWarner Losh  */
7239dc70af8SWarner Losh static int
command_fdt(int argc,char * argv[])7249dc70af8SWarner Losh command_fdt(int argc, char *argv[])
7259dc70af8SWarner Losh {
7269dc70af8SWarner Losh 
7279dc70af8SWarner Losh 	return (command_fdt_internal(argc, argv));
7289dc70af8SWarner Losh }
7299dc70af8SWarner Losh 
7309dc70af8SWarner Losh COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
7319dc70af8SWarner Losh #endif
732