199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
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 *
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 * are met:
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 *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "api_public.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "glue.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libuboot.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef nitems
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define nitems(x) (sizeof((x)) / sizeof((x)[0]))
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct uboot_devdesc currdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct arch_switch archsw; /* MI/MD interface boundary */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint devs_no;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuintptr_t uboot_heap_start;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuintptr_t uboot_heap_end;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct device_type {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome} device_types[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "disk", DEV_TYP_STOR },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "ide", DEV_TYP_STOR | DT_STOR_IDE },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "mmc", DEV_TYP_STOR | DT_STOR_MMC },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "sata", DEV_TYP_STOR | DT_STOR_SATA },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "scsi", DEV_TYP_STOR | DT_STOR_SCSI },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "usb", DEV_TYP_STOR | DT_STOR_USB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "net", DEV_TYP_NET }
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern char end[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern char bootprog_name[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern char bootprog_rev[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern char bootprog_date[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern char bootprog_maker[];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_FDT_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern int command_fdt_internal(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomedump_sig(struct api_signature *sig)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("signature:\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" version\t= %d\n", sig->version);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" checksum\t= 0x%08x\n", sig->checksum);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" sc entry\t= 0x%08x\n", sig->syscall);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomedump_addr_info(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\naddresses info:\n");
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(" _end = 0x%08x\n", (uint32_t)_end);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" syscall entry = 0x%08x\n", (uint32_t)syscall_ptr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic uint64_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomememsize(struct sys_info *si, int flags)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < si->mr_no; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (si->mr[i].flags == flags && si->mr[i].size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome size += (si->mr[i].size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomememinfo(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct sys_info *si;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((si = ub_get_sys_info()) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("could not retrieve system info");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < 3; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = memsize(si, t[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: %juMB\n", ub_mem_type(t[i]),
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uintmax_t)(size / 1024 / 1024));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_device_type(const char *devstr, int *devtype)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int namelen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct device_type *dt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (devstr) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < nitems(device_types); i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome dt = &device_types[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome namelen = strlen(dt->name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strncmp(dt->name, devstr, namelen) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *devtype = dt->type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (devstr + namelen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Unknown device type '%s'\n", devstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *devtype = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomedevice_typename(int type)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < nitems(device_types); i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (device_types[i].type == type)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (device_types[i].name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ("<unknown>");
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The returned values for slice and partition are interpreted by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * disk_open().
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Valid device strings: For device types:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * For valid type names, see the device_types array, above.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Slice numbers are 1-based. 0 is a wildcard.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeget_load_device(int *type, int *unit, int *slice, int *partition)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *devstr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *endp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *type = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *unit = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *slice = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *partition = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome devstr = ub_env_get("loaderdev");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (devstr == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("U-Boot env: loaderdev not set, will probe all devices.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("U-Boot env: loaderdev='%s'\n", devstr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = get_device_type(devstr, type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Ignore optional spaces after the device name. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p == ' ')
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Unknown device name, or a known name without unit number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((*type == -1) || (*p == '\0')) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Malformed unit number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!isdigit(*p)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *type = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Guaranteed to extract a number from the string, as *p is a digit. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *unit = strtol(p, &endp, 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = endp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Known device name with unit number and nothing else. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Device string is malformed beyond unit number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != ':') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *type = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *unit = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* No slice and partition specification. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ('\0' == *p )
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Only DEV_TYP_STOR devices can have a slice specification. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(*type & DEV_TYP_STOR)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *type = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *unit = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *slice = strtoul(p, &endp, 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Malformed slice number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p == endp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *type = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *unit = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *slice = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = endp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* No partition specification. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Device string is malformed beyond slice number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p != '.') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *type = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *unit = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *slice = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* No partition specification. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*p == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *partition = strtol(p, &endp, 10);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = endp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Full, valid device string. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*endp == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Junk beyond partition number. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *type = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *unit = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *slice = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *partition = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprint_disk_probe_info()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char slice[32];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char partition[32];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (currdev.d_disk.slice > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(slice, "%d", currdev.d_disk.slice);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(slice, "<auto>");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (currdev.d_disk.partition >= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(partition, "%d", currdev.d_disk.partition);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(partition, "<auto>");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" Checking unit=%d slice=%s partition=%s...",
199767f8919635c4928607450d9e0abb932109ceToomas Soome currdev.d_unit, slice, partition);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprobe_disks(int devidx, int load_type, int load_unit, int load_slice,
199767f8919635c4928607450d9e0abb932109ceToomas Soome int load_partition)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int open_result, unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_file f;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome currdev.d_disk.slice = load_slice;
199767f8919635c4928607450d9e0abb932109ceToomas Soome currdev.d_disk.partition = load_partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome f.f_devdata = &currdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome open_result = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (load_type == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" Probing all disk devices...\n");
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 currdev.d_unit++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome print_disk_probe_info();
199767f8919635c4928607450d9e0abb932109ceToomas Soome open_result = devsw[devidx]->dv_open(&f, &currdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (open_result == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" good.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (load_unit == -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 for (unit = 0; unit < UB_MAX_DEV; unit++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome currdev.d_unit = uboot_diskgetunit(load_type, unit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (currdev.d_unit == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome print_disk_probe_info();
199767f8919635c4928607450d9e0abb932109ceToomas Soome open_result = devsw[devidx]->dv_open(&f, &currdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (open_result == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" good.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((currdev.d_unit = uboot_diskgetunit(load_type, load_unit)) != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome print_disk_probe_info();
199767f8919635c4928607450d9e0abb932109ceToomas Soome open_result = devsw[devidx]->dv_open(&f,&currdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (open_result == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" good.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" Requested disk type/unit/slice/partition not found\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomemain(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct api_signature *sig = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int load_type, load_unit, load_slice, load_partition;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *ldev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!api_search_sig(&sig))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0x01badab1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome syscall_ptr = sig->syscall;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (syscall_ptr == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0x02badab1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sig->version > API_SIG_VERSION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0x03badab1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Clear BSS sections */
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(__sbss_start, __sbss_end - __sbss_start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(__bss_start, _end - __bss_start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uboot_heap_start = round_page((uintptr_t)end);
199767f8919635c4928607450d9e0abb932109ceToomas Soome uboot_heap_end = uboot_heap_start + 512 * 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome setheap((void *)uboot_heap_start, (void *)uboot_heap_end);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Set up console.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cons_probe();
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Compatible U-Boot API signature found @%p\n", sig);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("(%s, %s)\n", bootprog_maker, bootprog_date);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dump_sig(sig);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dump_addr_info();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome meminfo();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Enumerate U-Boot devices
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((devs_no = ub_dev_enum()) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("no U-Boot devices found");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Number of U-Boot devices: %d\n", devs_no);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome get_load_device(&load_type, &load_unit, &load_slice, &load_partition);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * March through the device switch probing for things.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; devsw[i] != NULL; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (devsw[i]->dv_init == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((devsw[i]->dv_init)() != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Found U-Boot device: %s\n", devsw[i]->dv_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome currdev.d_dev = devsw[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome currdev.d_type = currdev.d_dev->dv_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome currdev.d_unit = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((load_type == -1 || (load_type & DEV_TYP_STOR)) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcmp(devsw[i]->dv_name, "disk") == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (probe_disks(i, load_type, load_unit, load_slice,
199767f8919635c4928607450d9e0abb932109ceToomas Soome load_partition) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((load_type == -1 || (load_type & DEV_TYP_NET)) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcmp(devsw[i]->dv_name, "net") == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (devsw[i] == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("No boot device found!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0xbadef1ce);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ldev = uboot_fmtdev(&currdev);
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 Soome printf("Booting from %s\n", ldev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("LINES", "24", 1); /* optional */
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("prompt", "loader>", 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_loadaddr = uboot_loadaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_getdev = uboot_getdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin = uboot_copyin;
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyout = uboot_copyout;
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_readin = uboot_readin;
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_autoload = uboot_autoload;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome interact(NULL); /* doesn't return */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_heap(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("heap base at %p, top at %p, used %td\n", end, sbrk(0),
199767f8919635c4928607450d9e0abb932109ceToomas Soome sbrk(0) - end);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_reboot(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Resetting...\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub_reset();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Reset failed!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome while(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_devinfo(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((devs_no = ub_dev_enum()) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "no U-Boot devices found!?";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("U-Boot devices:\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < devs_no; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub_dump_di(i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_sysinfo(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct sys_info *si;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((si = ub_get_sys_info()) == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "could not retrieve U-Boot sys info!?";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("U-Boot system info:\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome ub_dump_si(si);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeenum ubenv_action {
199767f8919635c4928607450d9e0abb932109ceToomas Soome UBENV_UNKNOWN,
199767f8919635c4928607450d9e0abb932109ceToomas Soome UBENV_SHOW,
199767f8919635c4928607450d9e0abb932109ceToomas Soome UBENV_IMPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomehandle_uboot_env_var(enum ubenv_action action, const char * var)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char ldvar[128];
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *val;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *wrk;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (action == UBENV_IMPORT) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strcspn(var, "=");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("name cannot start with '=': '%s'\n", var);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (var[len] == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(ldvar, "uboot.");
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncat(ldvar, var, sizeof(ldvar) - 7);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = MIN(len, sizeof(ldvar) - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncpy(ldvar, var, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ldvar[len] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome var = &var[len + 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strncmp(var, "uboot.", 6) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome var = &var[6];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If there is no variable name left, punt. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (var[0] == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("empty variable name\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome val = ub_env_get(var);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (action == UBENV_SHOW) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (val == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("uboot.%s is not set\n", var);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("uboot.%s=%s\n", var, val);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (action == UBENV_IMPORT) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (val != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv(ldvar, val, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_ubenv(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome enum ubenv_action action;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *var;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome action = UBENV_UNKNOWN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc > 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcasecmp(argv[1], "import") == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome action = UBENV_IMPORT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (strcasecmp(argv[1], "show") == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome action = UBENV_SHOW;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (action == UBENV_UNKNOWN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "usage: 'ubenv <import|show> [var ...]";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc > 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 2; i < argc; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome handle_uboot_env_var(action, argv[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome var = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((var = ub_env_enum(var)) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome handle_uboot_env_var(action, var);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_FDT_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_fdt(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (command_fdt_internal(argc, argv));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif