199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2009-2010 The FreeBSD Foundation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software was developed by Semihalf under sponsorship from
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the FreeBSD Foundation.
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 AUTHOR 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
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <fdt.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <libfdt.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/linker.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/elf.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "fdt_platform.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define debugf(fmt, args...) do { printf("%s(): ", __func__); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(fmt,##args); } while (0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define debugf(fmt, args...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define FDT_CWD_LEN 256
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define FDT_MAX_DEPTH 6
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define FDT_PROP_SEP " = "
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COPYOUT(s,d,l) archsw.arch_copyout(s, d, l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COPYIN(s,d,l) archsw.arch_copyin(s, d, l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define FDT_STATIC_DTB_SYMBOL "fdt_static_dtb"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define CMD_REQUIRES_BLOB 0x01
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Location of FDT yet to be loaded. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* This may be in read-only memory, so can't be manipulated directly. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct fdt_header *fdt_to_load = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Location of FDT on heap. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* This is the copy we actually manipulate. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct fdt_header *fdtp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Size of FDT blob */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic size_t fdtp_size = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Location of FDT in kernel or module. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* This won't be set if FDT is loaded from disk or memory. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* If it is set, we'll update it when fdt_copy() gets called. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vm_offset_t fdtp_va = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_load_dtb(vm_offset_t va);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_nyi(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_addr(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_mkprop(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_cd(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_hdr(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_ls(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_prop(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_pwd(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_rm(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_mknode(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fdt_cmd_mres(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef int cmdf_t(int, char *[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct cmdtab {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmdf_t *handler;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int flags;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const struct cmdtab commands[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "addr", &fdt_cmd_addr, 0 },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "alias", &fdt_cmd_nyi, 0 },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "cd", &fdt_cmd_cd, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "header", &fdt_cmd_hdr, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "ls", &fdt_cmd_ls, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "mknode", &fdt_cmd_mknode, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "mkprop", &fdt_cmd_mkprop, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "mres", &fdt_cmd_mres, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "prop", &fdt_cmd_prop, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "pwd", &fdt_cmd_pwd, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { "rm", &fdt_cmd_rm, CMD_REQUIRES_BLOB },
199767f8919635c4928607450d9e0abb932109ceToomas Soome { NULL, NULL }
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char cwd[FDT_CWD_LEN] = "/";
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vm_offset_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_find_static_dtb()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Ehdr *ehdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Shdr *shdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome Elf_Sym sym;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t strtab, symtab, fdt_start;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t offs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *kfp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file_metadata *md;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *strp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, sym_count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("fdt_find_static_dtb()\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sym_count = symtab = strtab = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome strp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome offs = __elfN(relocation_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome kfp = file_findfile(NULL, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kfp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Locate the dynamic symbols and strtab. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome md = file_findmetadata(kfp, MODINFOMD_ELFHDR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (md == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ehdr = (Elf_Ehdr *)md->md_data;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome md = file_findmetadata(kfp, MODINFOMD_SHDR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (md == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome shdr = (Elf_Shdr *)md->md_data;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < ehdr->e_shnum; ++i) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (shdr[i].sh_type == SHT_DYNSYM && symtab == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome symtab = shdr[i].sh_addr + offs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sym_count = shdr[i].sh_size / sizeof(Elf_Sym);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (shdr[i].sh_type == SHT_STRTAB && strtab == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome strtab = shdr[i].sh_addr + offs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The most efficent way to find a symbol would be to calculate a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * hash, find proper bucket and chain, and thus find a symbol.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * However, that would involve code duplication (e.g. for hash
199767f8919635c4928607450d9e0abb932109ceToomas Soome * function). So we're using simpler and a bit slower way: we're
199767f8919635c4928607450d9e0abb932109ceToomas Soome * iterating through symbols, searching for the one which name is
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 'equal' to 'fdt_static_dtb'. To speed up the process a little bit,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * we are eliminating symbols type of which is not STT_NOTYPE, or(and)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * those which binding attribute is not STB_GLOBAL.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_start = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (sym_count > 0 && fdt_start == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(symtab, &sym, sizeof(sym));
199767f8919635c4928607450d9e0abb932109ceToomas Soome symtab += sizeof(sym);
199767f8919635c4928607450d9e0abb932109ceToomas Soome --sym_count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ELF_ST_TYPE(sym.st_info) != STT_NOTYPE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome strp = strdupout(strtab + sym.st_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(strp, FDT_STATIC_DTB_SYMBOL) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_start = (vm_offset_t)sym.st_value + offs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(strp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (fdt_start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_load_dtb(vm_offset_t va)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fdt_header header;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("fdt_load_dtb(0x%08jx)\n", (uintmax_t)va);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(va, &header, sizeof(header));
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = fdt_check_header(&header);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err == -FDT_ERR_BADVERSION) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "incompatible blob version: %d, should be: %d",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "error validating blob: %s", fdt_strerror(err));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Release previous blob
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdtp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fdtp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdtp_size = fdt_totalsize(&header);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdtp = malloc(fdtp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdtp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "can't allocate memory for device tree copy";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdtp_va = va;
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYOUT(va, fdtp, fdtp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_load_dtb_addr(struct fdt_header *header)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("fdt_load_dtb_addr(%p)\n", header);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdtp_size = fdt_totalsize(header);
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = fdt_check_header(header);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "error validating blob: %s", fdt_strerror(err));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(fdtp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((fdtp = malloc(fdtp_size)) == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "can't allocate memory for device tree copy";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdtp_va = 0; // Don't write this back into module or kernel.
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(header, fdtp, fdtp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_load_dtb_file(const char * filename)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *bfp, *oldbfp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("fdt_load_dtb_file(%s)\n", filename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome oldbfp = file_findfile(NULL, "dtb");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Attempt to load and validate a new dtb from a file. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((bfp = file_loadraw(filename, "dtb", 1)) == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "failed to load file '%s'", filename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = fdt_load_dtb(bfp->f_addr)) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_discard(bfp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* A new dtb was validated, discard any previous file. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (oldbfp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_discard(oldbfp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_setup_fdtp()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *bfp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t va;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("fdt_setup_fdtp()\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If we already loaded a file, use it. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_load_dtb(bfp->f_addr) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Using DTB from loaded file '%s'.\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome bfp->f_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If we were given the address of a valid blob in memory, use it. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_to_load != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_load_dtb_addr(fdt_to_load) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Using DTB from memory address 0x%p.\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_to_load);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_platform_load_dtb() == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If there is a dtb compiled into the kernel, use it. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((va = fdt_find_static_dtb()) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_load_dtb(va) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Using DTB compiled into kernel.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "No device tree blob found!\n";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
199767f8919635c4928607450d9e0abb932109ceToomas Soome (cellbuf), (lim), (cellsize), 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Force using base 16 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
199767f8919635c4928607450d9e0abb932109ceToomas Soome (cellbuf), (lim), (cellsize), 16);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soome_fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize,
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t base)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *buf = str;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *end = str + strlen(str) - 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t *u32buf = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t *u8buf = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int cnt = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cellsize == sizeof(uint32_t))
199767f8919635c4928607450d9e0abb932109ceToomas Soome u32buf = (uint32_t *)cellbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome u8buf = (uint8_t *)cellbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lim == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (buf < end) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Skip white whitespace(s)/separators */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (!isxdigit(*buf) && buf < end)
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (u32buf != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome u32buf[cnt] =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cnt + 1 <= lim - 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome cnt++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find another number */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((isxdigit(*buf) || *buf == 'x') && buf < end)
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (cnt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_fixup_ethernet(const char *str, char *ethstr, int len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t tmp_addr[6];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Convert macaddr string into a vector of uints */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set actual property to a value from vect */
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int lo, o = 0, o2, maxo = 0, depth;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const uint32_t zero = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We want to modify every subnode of /cpus */
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_path_offset(fdtp, "/cpus");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (o < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* maxo should contain offset of node next to /cpus */
199767f8919635c4928607450d9e0abb932109ceToomas Soome depth = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome maxo = o;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (depth != -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome maxo = fdt_next_node(fdtp, maxo, &depth);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find CPU frequency properties */
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
199767f8919635c4928607450d9e0abb932109ceToomas Soome &zero, sizeof(uint32_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(uint32_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome lo = MIN(o, o2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_node_offset_by_prop_value(fdtp, lo,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "clock-frequency", &zero, sizeof(uint32_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
199767f8919635c4928607450d9e0abb932109ceToomas Soome &zero, sizeof(uint32_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We're only interested in /cpus subnode(s) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lo > maxo)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uint32_t)cpufreq);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uint32_t)busfreq);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome lo = MIN(o, o2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef notyet
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int cells_in_tuple, i, tuples, tuple_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t cur_start, cur_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cells_in_tuple = (addr_cells + size_cells);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tuple_size = cells_in_tuple * sizeof(uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tuples = len / tuple_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tuples == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < tuples; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (addr_cells == 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size_cells == 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cur_size == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome i, cur_start, cur_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_fixup_memory(struct fdt_mem_region *region, size_t num)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fdt_mem_region *curmr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t addr_cells, size_cells;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t *addr_cellsp, *size_cellsp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err, i, len, memory, root;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t realmrno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t *buf, *sb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t rstart, rsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int reserved;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome root = fdt_path_offset(fdtp, "/");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (root < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "Could not find root node !");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome memory = fdt_path_offset(fdtp, "/memory");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (memory <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Create proper '/memory' node. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome memory = fdt_add_subnode(fdtp, root, "memory");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (memory <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Could not fixup '/memory' "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "node, error code : %d!\n", memory);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = fdt_setprop(fdtp, memory, "device_type", "memory",
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof("memory"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
199767f8919635c4928607450d9e0abb932109ceToomas Soome NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (addr_cellsp == NULL || size_cellsp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Could not fixup '/memory' node : "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "%s %s property not found in root node!\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (!addr_cellsp) ? "#address-cells" : "",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (!size_cellsp) ? "#size-cells" : "");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr_cells = fdt32_to_cpu(*addr_cellsp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_cells = fdt32_to_cpu(*size_cellsp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Convert memreserve data to memreserve property
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check if property already exists
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome reserved = fdt_num_mem_rsv(fdtp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (reserved &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb = buf = (uint8_t *)malloc(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(buf, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < reserved; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rsize) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Ensure endianess, and put cells into a buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (addr_cells == 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(uint64_t *)buf =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt64(rstart);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(uint32_t *)buf =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt32(rstart);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf += sizeof(uint32_t) * addr_cells;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size_cells == 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(uint64_t *)buf =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt64(rsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(uint32_t *)buf =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt32(rsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf += sizeof(uint32_t) * size_cells;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set property */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Could not fixup 'memreserve' property.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(sb);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Count valid memory regions entries in sysinfo. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome realmrno = num;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < num; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (region[i].start == 0 && region[i].size == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome realmrno--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (realmrno == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Could not fixup '/memory' node : "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "sysinfo doesn't contain valid memory regions info!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sb = buf = (uint8_t *)malloc(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(buf, len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < num; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome curmr = &region[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (curmr->size != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Ensure endianess, and put cells into a buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (addr_cells == 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(uint64_t *)buf =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt64(curmr->start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(uint32_t *)buf =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt32(curmr->start);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf += sizeof(uint32_t) * addr_cells;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size_cells == 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(uint64_t *)buf =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt64(curmr->size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(uint32_t *)buf =
199767f8919635c4928607450d9e0abb932109ceToomas Soome cpu_to_fdt32(curmr->size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf += sizeof(uint32_t) * size_cells;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set property */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(sb);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_fixup_stdout(const char *str)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *ptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int serialno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len, no, sero;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const struct fdt_property *prop;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *tmp[10];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ptr = (char *)str + strlen(str) - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (ptr > str && isdigit(*(str - 1)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome str--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ptr == str)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome serialno = (int)strtol(ptr, NULL, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome no = fdt_path_offset(fdtp, "/chosen");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (no < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome prop = fdt_get_property(fdtp, no, "stdout", &len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If /chosen/stdout does not extist, create it */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (prop == NULL || (prop != NULL && len == 0)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(tmp, 10 * sizeof(char));
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy((char *)&tmp, "serial");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strlen(ptr) > 3)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Serial number too long */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncpy((char *)tmp + 6, ptr, 3);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sero = fdt_path_offset(fdtp, (const char *)tmp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sero < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If serial device we're trying to assign
199767f8919635c4928607450d9e0abb932109ceToomas Soome * stdout to doesn't exist in DT -- return.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_setprop(fdtp, no, "stdout", &tmp,
199767f8919635c4928607450d9e0abb932109ceToomas Soome strlen((char *)&tmp) + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_setprop(fdtp, no, "stdin", &tmp,
199767f8919635c4928607450d9e0abb932109ceToomas Soome strlen((char *)&tmp) + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Locate the blob, fix it up and return its location.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_fixup(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int chosen, len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("fdt_fixup()\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdtp == NULL && fdt_setup_fdtp() != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Create /chosen node (if not exists) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
199767f8919635c4928607450d9e0abb932109ceToomas Soome -FDT_ERR_NOTFOUND)
199767f8919635c4928607450d9e0abb932109ceToomas Soome chosen = fdt_add_subnode(fdtp, 0, "chosen");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Value assigned to fixup-applied does not matter. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_platform_fixups();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copy DTB blob to specified location and return size
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_copy(vm_offset_t va)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome debugf("fdt_copy va 0x%08x\n", va);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdtp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = fdt_setup_fdtp();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("No valid device tree blob found!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_fixup() == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdtp_va != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Overwrite the FDT with the fixed version. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX Is this really appropriate? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYIN(fdtp, fdtp_va, fdtp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPYIN(fdtp, va, fdtp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (fdtp_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_fdt_internal(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmdf_t *cmdh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int flags;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cmd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc < 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "usage is 'fdt <command> [<args>]";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Validate fdt <command>.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmd = strdup(argv[1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome i = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmdh = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (!(commands[i].name == NULL)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(cmd, commands[i].name) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* found it */
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmdh = commands[i].handler;
199767f8919635c4928607450d9e0abb932109ceToomas Soome flags = commands[i].flags;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome i++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cmdh == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "unknown command";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (flags & CMD_REQUIRES_BLOB) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check if uboot env vars were parsed already. If not, do it now.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_fixup() == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Call command handler.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = (*cmdh)(argc, argv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_addr(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fdt_header *hdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_to_load = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc > 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = argv[2];
199767f8919635c4928607450d9e0abb932109ceToomas Soome else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "no address specified");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cp == addr) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Invalid address: %s", addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((fp = file_findfile(NULL, "dtb")) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_discard(fp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_to_load = hdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_cd(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char tmp[FDT_CWD_LEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len, o;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = (argc > 2) ? argv[2] : "/";
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path[0] == '/') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len >= FDT_CWD_LEN)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto fail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Handle path specification relative to cwd */
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(cwd) + strlen(path) + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len >= FDT_CWD_LEN)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto fail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(tmp, cwd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(tmp, "/");
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(tmp, path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = tmp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_path_offset(fdtp, path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (o < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "could not find node: '%s'", path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(cwd, path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomefail:
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "path too long: %d, max allowed: %d", len, FDT_CWD_LEN - 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_hdr(int argc __unused, char *argv[] __unused)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ver;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdtp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "no device tree blob pointer?!";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ver = fdt_version(fdtp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " off_dt_struct = 0x%08x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_off_dt_struct(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " off_dt_strings = 0x%08x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_off_dt_strings(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " off_mem_rsvmap = 0x%08x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_off_mem_rsvmap(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " version = %d\n", ver);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " last compatible version = %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_last_comp_version(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ver >= 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " boot_cpuid = %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_boot_cpuid_phys(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ver >= 3) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " size_dt_strings = %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_size_dt_strings(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ver >= 17) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, " size_dt_struct = %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_size_dt_struct(fdtp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_ls(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *prevname[FDT_MAX_DEPTH] = { NULL };
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, o, depth, len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = (argc > 2) ? argv[2] : NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = cwd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_path_offset(fdtp, path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (o < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "could not find node: '%s'", path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (depth = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome (o >= 0) && (depth >= 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_next_node(fdtp, o, &depth)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome name = fdt_get_name(fdtp, o, &len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (depth > FDT_MAX_DEPTH) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("max depth exceeded: %d\n", depth);
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome prevname[depth] = name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Skip root (i = 1) when printing devices */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 1; i <= depth; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (prevname[i] == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(cwd, "/") == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("/");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s", prevname[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic __inline int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeisprint(int c)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (c >= ' ' && c <= 0x7e);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_isprint(const void *data, int len, int *count)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char ch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int yesno, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome d = (const char *)data;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (d[len - 1] != '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *count = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome yesno = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < len; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ch = *(d + i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (isprint(ch) || (ch == '\0' && i > 0)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Count strings */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ch == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome (*count)++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome yesno = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (yesno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_data_str(const void *data, int len, int count, char **buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *b, *tmp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int buf_len, i, l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Calculate the length for the string and allocate memory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that 'len' already includes at least one terminator.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf_len = len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (count > 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Each token had already a terminator buried in 'len', but we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * only need one eventually, don't count space for these.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf_len -= count - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each consecutive token requires a ", " separator. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf_len += count * 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Add some space for surrounding double quotes. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf_len += count * 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome b = (char *)malloc(buf_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp = (char *)malloc(buf_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tmp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(b);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome b[0] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Now that we have space, format the string.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome i = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome d = (const char *)data + i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l = strlen(d) + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(tmp, "\"%s\"%s", d,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (i + l) < len ? ", " : "");
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(b, tmp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome i += l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (i < len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buf = b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tmp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeerror:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_data_cell(const void *data, int len, char **buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *b, *tmp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const uint32_t *c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int count, i, l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Number of cells */
199767f8919635c4928607450d9e0abb932109ceToomas Soome count = len / 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Calculate the length for the string and allocate memory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each byte translates to 2 output characters */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l = len * 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (count > 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each consecutive cell requires a " " separator. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l += (count - 1) * 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each cell will have a "0x" prefix */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l += count * 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Space for surrounding <> and terminator */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l += 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome b = (char *)malloc(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp = (char *)malloc(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tmp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(b);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome b[0] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(b, "<");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < len; i += 4) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = (const uint32_t *)((const uint8_t *)data + i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
199767f8919635c4928607450d9e0abb932109ceToomas Soome i < (len - 4) ? " " : "");
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(b, tmp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(b, ">");
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buf = b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tmp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeerror:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_data_bytes(const void *data, int len, char **buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *b, *tmp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Calculate the length for the string and allocate memory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each byte translates to 2 output characters */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l = len * 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len > 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each consecutive byte requires a " " separator. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l += (len - 1) * 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Each byte will have a "0x" prefix */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l += len * 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Space for surrounding [] and terminator. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l += 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome b = (char *)malloc(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tmp = (char *)malloc(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tmp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(b);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome b[0] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(b, "[");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0, d = data; i < len; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(b, tmp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcat(b, "]");
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buf = b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tmp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeerror:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_data_fmt(const void *data, int len, char **buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buf = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_isprint(data, len, &count))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (fdt_data_str(data, len, count, buf));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if ((len % 4) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (fdt_data_cell(data, len, buf));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (fdt_data_bytes(data, len, buf));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_prop(int offset)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *line, *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const struct fdt_property *prop;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const void *data;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len, rv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome line = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (prop == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = fdt32_to_cpu(prop->len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Property without value */
199767f8919635c4928607450d9e0abb932109ceToomas Soome line = (char *)malloc(strlen(name) + 2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (line == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "%s\n", name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Process property with value
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome data = prop->data;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_data_fmt(data, len, &buf) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = 3;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
199767f8919635c4928607450d9e0abb932109ceToomas Soome strlen(buf) + 2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (line == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "could not allocate space for string");
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout1:
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout2:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (line)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_modprop(int nodeoff, char *propname, void *value, char mode)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t cells[100];
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len, rv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const struct fdt_property *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = fdt_get_property(fdtp, nodeoff, propname, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (p != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (mode == 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Adding inexistant value in mode 1 is forbidden */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "property already exists!");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (mode == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "property does not exist!");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = value;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (*buf) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '&':
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* phandles */
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '<':
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Data cells */
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = fdt_strtovect(buf, (void *)&cells, 100,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(uint32_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
199767f8919635c4928607450d9e0abb932109ceToomas Soome len * sizeof(uint32_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '[':
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Data bytes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = fdt_strtovect(buf, (void *)&cells, 100,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(uint8_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
199767f8919635c4928607450d9e0abb932109ceToomas Soome len * sizeof(uint8_t));
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '"':
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Default -- string */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rv != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rv == -FDT_ERR_NOSPACE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Device tree blob is too small!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Could not add/modify property!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Merge strings from argv into a single string */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_merge_strings(int argc, char *argv[], int start, char **buffer)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, idx, sz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buffer = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = start; i < argc; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz += strlen(argv[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Additional bytes for whitespaces between args */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz += argc - start;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = (char *)malloc(sizeof(char) * sz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "could not allocate space "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "for string");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(buf, sizeof(char) * sz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome idx = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = start, idx = 0; i < argc; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(buf + idx, argv[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome idx += strlen(argv[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[idx] = ' ';
199767f8919635c4928607450d9e0abb932109ceToomas Soome idx++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[sz - 1] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome *buffer = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Extract offset and name of node/property from a given path */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int o;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path = *pathp, *name = NULL, *subpath = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome subpath = strrchr(path, '/');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (subpath == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_path_offset(fdtp, cwd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome name = path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = (char *)&cwd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *subpath = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strlen(path) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = cwd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome name = subpath + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_path_offset(fdtp, path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strlen(name) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "name not specified");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (o < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "could not find node: '%s'", path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome *namep = name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *nodeoff = o;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *pathp = path;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_prop(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path, *propname, *value;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int o, next, depth, rv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t tag;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = (argc > 2) ? argv[2] : NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome value = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc > 3) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Merge property value strings into one */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_merge_strings(argc, argv, 3, &value) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome value = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = cwd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = CMD_OK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (value) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If value is specified -- try to modify prop. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_extract_nameloc(&path, &propname, &o) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = fdt_modprop(o, propname, value, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rv)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* User wants to display properties */
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_path_offset(fdtp, path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (o < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "could not find node: '%s'", path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = CMD_ERROR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome depth = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (depth >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tag = fdt_next_tag(fdtp, o, &next);
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (tag) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case FDT_NOP:
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case FDT_PROP:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (depth > 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Don't process properties of nested nodes */
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_prop(o) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "could not process "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "property");
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = CMD_ERROR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case FDT_BEGIN_NODE:
199767f8919635c4928607450d9e0abb932109ceToomas Soome depth++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (depth > FDT_MAX_DEPTH) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("warning: nesting too deep: %d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome depth);
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case FDT_END_NODE:
199767f8919635c4928607450d9e0abb932109ceToomas Soome depth--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (depth == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is the end of our starting node, force
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the loop finish.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome depth--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = next;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_mkprop(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int o;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path, *propname, *value;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = (argc > 2) ? argv[2] : NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome value = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc > 3) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Merge property value strings into one */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_merge_strings(argc, argv, 3, &value) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome value = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_extract_nameloc(&path, &propname, &o) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_modprop(o, propname, value, 1))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_rm(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int o, rv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path = NULL, *propname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc > 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = argv[2];
199767f8919635c4928607450d9e0abb932109ceToomas Soome else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "no node/property name specified");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome o = fdt_path_offset(fdtp, path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (o < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If node not found -- try to find & delete property */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_extract_nameloc(&path, &propname, &o) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "could not delete %s\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (rv == -FDT_ERR_NOTFOUND) ?
199767f8919635c4928607450d9e0abb932109ceToomas Soome "(property/node does not exist)" : "");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If node exists -- remove node */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = fdt_del_node(fdtp, o);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rv) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "could not delete node");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_mknode(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int o, rv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *path = NULL, *nodename = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc > 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome path = argv[2];
199767f8919635c4928607450d9e0abb932109ceToomas Soome else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf, "no node name specified");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rv = fdt_add_subnode(fdtp, o, nodename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rv < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rv == -FDT_ERR_NOSPACE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Device tree blob is too small!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(command_errbuf,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Could not add node!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_pwd(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[FDT_CWD_LEN];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "%s\n", cwd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_mres(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t start, size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, total;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome total = fdt_num_mem_rsv(fdtp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (total > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output("Reserved memory regions:\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < total; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fdt_get_mem_rsv(fdtp, i, &start, &size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome i, start, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output("No reserved memory regions\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefdt_cmd_nyi(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("command not yet implemented\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}