main.c revision 199767f8919635c4928607450d9e0abb932109ce
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2000 Benno Rice <benno@jeamland.net>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define nitems(x) (sizeof((x)) / sizeof((x)[0]))
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct arch_switch archsw; /* MI/MD interface boundary */
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *name;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern char end[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern unsigned char _etext[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern unsigned char _edata[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern unsigned char __bss_start[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern unsigned char __sbss_start[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern unsigned char __sbss_end[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern unsigned char _end[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern int command_fdt_internal(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" checksum\t= 0x%08x\n", sig->checksum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" _edata = 0x%08x\n", (uint32_t)_edata);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" __sbss_start = 0x%08x\n", (uint32_t)__sbss_start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" __sbss_end = 0x%08x\n", (uint32_t)__sbss_end);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" __sbss_start = 0x%08x\n", (uint32_t)__bss_start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" syscall entry = 0x%08x\n", (uint32_t)syscall_ptr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (si->mr[i].flags == flags && si->mr[i].size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < 3; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_device_type(const char *devstr, int *devtype)
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char *
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ("<unknown>");
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Parse a device string into type, unit, slice and partition numbers. A
199767f8919635c4928607450d9e0abb932109ceToomas Soome * returned value of -1 for type indicates a search should be done for the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * first loadable device, otherwise a returned value of -1 for unit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * indicates a search should be done for the first loadable device of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * given type.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The returned values for slice and partition are interpreted by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * disk_open().
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Valid device strings: For device types:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * <type_name> DEV_TYP_STOR, DEV_TYP_NET
199767f8919635c4928607450d9e0abb932109ceToomas Soome * <type_name><unit> DEV_TYP_STOR, DEV_TYP_NET
199767f8919635c4928607450d9e0abb932109ceToomas Soome * <type_name><unit>: DEV_TYP_STOR, DEV_TYP_NET
199767f8919635c4928607450d9e0abb932109ceToomas Soome * <type_name><unit>:<slice> DEV_TYP_STOR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * <type_name><unit>:<slice>. DEV_TYP_STOR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * <type_name><unit>:<slice>.<partition> DEV_TYP_STOR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For valid type names, see the device_types array, above.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Slice numbers are 1-based. 0 is a wildcard.
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_load_device(int *type, int *unit, int *slice, int *partition)
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("U-Boot env: loaderdev not set, will probe all devices.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("U-Boot env: loaderdev='%s'\n", devstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Ignore optional spaces after the device name. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p == ' ')
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Unknown device name, or a known name without unit number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Malformed unit number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Guaranteed to extract a number from the string, as *p is a digit. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Known device name with unit number and nothing else. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Device string is malformed beyond unit number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != ':') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* No slice and partition specification. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ('\0' == *p )
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Only DEV_TYP_STOR devices can have a slice specification. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Malformed slice number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* No partition specification. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Device string is malformed beyond slice number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != '.') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* No partition specification. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Full, valid device string. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Junk beyond partition number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(partition, "%d", currdev.d_disk.partition);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" Checking unit=%d slice=%s partition=%s...",
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprobe_disks(int devidx, int load_type, int load_unit, int load_slice,
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try each disk in succession until one works. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (currdev.d_unit = 0; currdev.d_unit < UB_MAX_DEV;
199767f8919635c4928607450d9e0abb932109ceToomas Soome open_result = devsw[devidx]->dv_open(&f, &currdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" Probing all %s devices...\n", device_typename(load_type));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try each disk of given type in succession until one works. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome currdev.d_unit = uboot_diskgetunit(load_type, unit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome open_result = devsw[devidx]->dv_open(&f, &currdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((currdev.d_unit = uboot_diskgetunit(load_type, load_unit)) != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome open_result = devsw[devidx]->dv_open(&f,&currdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" Requested disk type/unit/slice/partition not found\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome int load_type, load_unit, load_slice, load_partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *ldev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If we can't find the magic signature and related info, exit with a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * unique error code that U-Boot reports as "## Application terminated,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * rc = 0xnnbadab1". Hopefully 'badab1' looks enough like "bad api" to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * provide a clue. It's better than 0xffffffff anyway.
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0x01badab1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0x02badab1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0x03badab1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Clear BSS sections */
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(__sbss_start, __sbss_end - __sbss_start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Initialise the heap as early as possible. Once this is done,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * alloc() is usable. The stack is buried inside us, so this is safe.
199767f8919635c4928607450d9e0abb932109ceToomas Soome uboot_heap_end = uboot_heap_start + 512 * 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome setheap((void *)uboot_heap_start, (void *)uboot_heap_end);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Set up console.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Compatible U-Boot API signature found @%p\n", sig);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("(%s, %s)\n", bootprog_maker, bootprog_date);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Enumerate U-Boot devices
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Number of U-Boot devices: %d\n", devs_no);
199767f8919635c4928607450d9e0abb932109ceToomas Soome get_load_device(&load_type, &load_unit, &load_slice, &load_partition);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * March through the device switch probing for things.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Found U-Boot device: %s\n", devsw[i]->dv_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((load_type == -1 || (load_type & DEV_TYP_STOR)) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (probe_disks(i, load_type, load_unit, load_slice,
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((load_type == -1 || (load_type & DEV_TYP_NET)) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If we couldn't find a boot device, return an error to u-boot.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * U-boot may be running a boot script that can try something different
199767f8919635c4928607450d9e0abb932109ceToomas Soome * so returning an error is better than forcing a reboot.
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0xbadef1ce);
199767f8919635c4928607450d9e0abb932109ceToomas Soome env_setenv("currdev", EV_VOLATILE, ldev, uboot_setcurrdev, env_nounset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome env_setenv("loaddev", EV_VOLATILE, ldev, env_noset, env_nounset);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("heap base at %p, top at %p, used %td\n", end, sbrk(0),
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < devs_no; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "could not retrieve U-Boot sys info!?";
199767f8919635c4928607450d9e0abb932109ceToomas Soomehandle_uboot_env_var(enum ubenv_action action, const char * var)
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *val;
199767f8919635c4928607450d9e0abb932109ceToomas Soome * On an import with the variable name formatted as ldname=ubname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * import the uboot variable ubname into the loader variable ldname,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * otherwise the historical behavior is to import to uboot.ubname.
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("name cannot start with '=': '%s'\n", var);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If the user prepended "uboot." (which is how they usually see these
199767f8919635c4928607450d9e0abb932109ceToomas Soome * names) strip it off as a convenience.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If there is no variable name left, punt. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (var[0] == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *var;
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "usage: 'ubenv <import|show> [var ...]";
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and declaring it as extern is in contradiction with COMMAND_SET() macro
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (which uses static pointer), we're defining wrapper function, which
199767f8919635c4928607450d9e0abb932109ceToomas Soome * calls the proper fdt handling routine.
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);