fdt_loader_cmd.c revision 265065
1208538Sraj/*- 2208538Sraj * Copyright (c) 2009-2010 The FreeBSD Foundation 3208538Sraj * All rights reserved. 4208538Sraj * 5208538Sraj * This software was developed by Semihalf under sponsorship from 6208538Sraj * the FreeBSD Foundation. 7208538Sraj * 8208538Sraj * Redistribution and use in source and binary forms, with or without 9208538Sraj * modification, are permitted provided that the following conditions 10208538Sraj * are met: 11208538Sraj * 1. Redistributions of source code must retain the above copyright 12208538Sraj * notice, this list of conditions and the following disclaimer. 13208538Sraj * 2. Redistributions in binary form must reproduce the above copyright 14208538Sraj * notice, this list of conditions and the following disclaimer in the 15208538Sraj * documentation and/or other materials provided with the distribution. 16208538Sraj * 17208538Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18208538Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19208538Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20208538Sraj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21208538Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22208538Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23208538Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24208538Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25208538Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26208538Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27208538Sraj * SUCH DAMAGE. 28208538Sraj */ 29208538Sraj 30208538Sraj#include <sys/cdefs.h> 31208538Sraj__FBSDID("$FreeBSD: stable/10/sys/boot/fdt/fdt_loader_cmd.c 265065 2014-04-29 00:09:48Z ian $"); 32208538Sraj 33208538Sraj#include <stand.h> 34208538Sraj#include <fdt.h> 35208538Sraj#include <libfdt.h> 36233230Sraj#include <sys/param.h> 37233230Sraj#include <sys/linker.h> 38233230Sraj#include <machine/elf.h> 39208538Sraj 40208538Sraj#include "bootstrap.h" 41208538Sraj#include "glue.h" 42208538Sraj 43208538Sraj#ifdef DEBUG 44208538Sraj#define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 45208538Sraj printf(fmt,##args); } while (0) 46208538Sraj#else 47208538Sraj#define debugf(fmt, args...) 48208538Sraj#endif 49208538Sraj 50208538Sraj#define FDT_CWD_LEN 256 51208538Sraj#define FDT_MAX_DEPTH 6 52208538Sraj 53208538Sraj#define FDT_PROP_SEP " = " 54208538Sraj 55208538Sraj#define STR(number) #number 56208538Sraj#define STRINGIFY(number) STR(number) 57208538Sraj 58235529Skientzle#define COPYOUT(s,d,l) archsw.arch_copyout(s, d, l) 59235529Skientzle#define COPYIN(s,d,l) archsw.arch_copyin(s, d, l) 60233230Sraj 61233230Sraj#define FDT_STATIC_DTB_SYMBOL "fdt_static_dtb" 62233230Sraj 63243693Sgonzo#define CMD_REQUIRES_BLOB 0x01 64243693Sgonzo 65247201Skientzle/* Location of FDT yet to be loaded. */ 66247250Skientzle/* This may be in read-only memory, so can't be manipulated directly. */ 67247201Skientzlestatic struct fdt_header *fdt_to_load = NULL; 68247250Skientzle/* Location of FDT on heap. */ 69247250Skientzle/* This is the copy we actually manipulate. */ 70208538Srajstatic struct fdt_header *fdtp = NULL; 71235529Skientzle/* Size of FDT blob */ 72235529Skientzlestatic size_t fdtp_size = 0; 73247250Skientzle/* Location of FDT in kernel or module. */ 74247250Skientzle/* This won't be set if FDT is loaded from disk or memory. */ 75247250Skientzle/* If it is set, we'll update it when fdt_copy() gets called. */ 76235529Skientzlestatic vm_offset_t fdtp_va = 0; 77208538Sraj 78243693Sgonzostatic int fdt_load_dtb(vm_offset_t va); 79243693Sgonzo 80208538Srajstatic int fdt_cmd_nyi(int argc, char *argv[]); 81208538Sraj 82243693Sgonzostatic int fdt_cmd_addr(int argc, char *argv[]); 83208538Srajstatic int fdt_cmd_mkprop(int argc, char *argv[]); 84208538Srajstatic int fdt_cmd_cd(int argc, char *argv[]); 85208538Srajstatic int fdt_cmd_hdr(int argc, char *argv[]); 86208538Srajstatic int fdt_cmd_ls(int argc, char *argv[]); 87208538Srajstatic int fdt_cmd_prop(int argc, char *argv[]); 88208538Srajstatic int fdt_cmd_pwd(int argc, char *argv[]); 89208538Srajstatic int fdt_cmd_rm(int argc, char *argv[]); 90208538Srajstatic int fdt_cmd_mknode(int argc, char *argv[]); 91243693Sgonzostatic int fdt_cmd_mres(int argc, char *argv[]); 92208538Sraj 93208538Srajtypedef int cmdf_t(int, char *[]); 94208538Sraj 95208538Srajstruct cmdtab { 96208538Sraj char *name; 97208538Sraj cmdf_t *handler; 98243693Sgonzo int flags; 99208538Sraj}; 100208538Sraj 101208538Srajstatic const struct cmdtab commands[] = { 102243693Sgonzo { "addr", &fdt_cmd_addr, 0 }, 103243693Sgonzo { "alias", &fdt_cmd_nyi, 0 }, 104243693Sgonzo { "cd", &fdt_cmd_cd, CMD_REQUIRES_BLOB }, 105243693Sgonzo { "header", &fdt_cmd_hdr, CMD_REQUIRES_BLOB }, 106243693Sgonzo { "ls", &fdt_cmd_ls, CMD_REQUIRES_BLOB }, 107243693Sgonzo { "mknode", &fdt_cmd_mknode, CMD_REQUIRES_BLOB }, 108243693Sgonzo { "mkprop", &fdt_cmd_mkprop, CMD_REQUIRES_BLOB }, 109243693Sgonzo { "mres", &fdt_cmd_mres, CMD_REQUIRES_BLOB }, 110243693Sgonzo { "prop", &fdt_cmd_prop, CMD_REQUIRES_BLOB }, 111243693Sgonzo { "pwd", &fdt_cmd_pwd, CMD_REQUIRES_BLOB }, 112243693Sgonzo { "rm", &fdt_cmd_rm, CMD_REQUIRES_BLOB }, 113208538Sraj { NULL, NULL } 114208538Sraj}; 115208538Sraj 116208538Srajstatic char cwd[FDT_CWD_LEN] = "/"; 117208538Sraj 118233230Srajstatic vm_offset_t 119235529Skientzlefdt_find_static_dtb() 120233230Sraj{ 121248121Sian Elf_Ehdr *ehdr; 122248121Sian Elf_Shdr *shdr; 123233230Sraj Elf_Sym sym; 124248121Sian vm_offset_t strtab, symtab, fdt_start; 125233230Sraj uint64_t offs; 126233230Sraj struct preloaded_file *kfp; 127233230Sraj struct file_metadata *md; 128235529Skientzle char *strp; 129248121Sian int i, sym_count; 130233230Sraj 131248934Skientzle sym_count = symtab = strtab = 0; 132235529Skientzle strp = NULL; 133233230Sraj 134233230Sraj offs = __elfN(relocation_offset); 135233230Sraj 136233230Sraj kfp = file_findfile(NULL, NULL); 137233230Sraj if (kfp == NULL) 138233230Sraj return (0); 139233230Sraj 140248121Sian /* Locate the dynamic symbols and strtab. */ 141248121Sian md = file_findmetadata(kfp, MODINFOMD_ELFHDR); 142233230Sraj if (md == NULL) 143233230Sraj return (0); 144248121Sian ehdr = (Elf_Ehdr *)md->md_data; 145233230Sraj 146248121Sian md = file_findmetadata(kfp, MODINFOMD_SHDR); 147233230Sraj if (md == NULL) 148233230Sraj return (0); 149248121Sian shdr = (Elf_Shdr *)md->md_data; 150233230Sraj 151248121Sian for (i = 0; i < ehdr->e_shnum; ++i) { 152248121Sian if (shdr[i].sh_type == SHT_DYNSYM && symtab == 0) { 153248121Sian symtab = shdr[i].sh_addr + offs; 154248121Sian sym_count = shdr[i].sh_size / sizeof(Elf_Sym); 155248121Sian } else if (shdr[i].sh_type == SHT_STRTAB && strtab == 0) { 156248121Sian strtab = shdr[i].sh_addr + offs; 157233230Sraj } 158233230Sraj } 159233230Sraj 160233230Sraj /* 161233230Sraj * The most efficent way to find a symbol would be to calculate a 162233230Sraj * hash, find proper bucket and chain, and thus find a symbol. 163233230Sraj * However, that would involve code duplication (e.g. for hash 164233230Sraj * function). So we're using simpler and a bit slower way: we're 165233230Sraj * iterating through symbols, searching for the one which name is 166233230Sraj * 'equal' to 'fdt_static_dtb'. To speed up the process a little bit, 167233230Sraj * we are eliminating symbols type of which is not STT_NOTYPE, or(and) 168233230Sraj * those which binding attribute is not STB_GLOBAL. 169233230Sraj */ 170235529Skientzle fdt_start = 0; 171235529Skientzle while (sym_count > 0 && fdt_start == 0) { 172235529Skientzle COPYOUT(symtab, &sym, sizeof(sym)); 173235529Skientzle symtab += sizeof(sym); 174235529Skientzle --sym_count; 175233230Sraj if (ELF_ST_BIND(sym.st_info) != STB_GLOBAL || 176233230Sraj ELF_ST_TYPE(sym.st_info) != STT_NOTYPE) 177233230Sraj continue; 178235529Skientzle strp = strdupout(strtab + sym.st_name); 179235529Skientzle if (strcmp(strp, FDT_STATIC_DTB_SYMBOL) == 0) 180235529Skientzle fdt_start = (vm_offset_t)sym.st_value + offs; 181233230Sraj free(strp); 182233230Sraj } 183235529Skientzle return (fdt_start); 184233230Sraj} 185233230Sraj 186208538Srajstatic int 187243693Sgonzofdt_load_dtb(vm_offset_t va) 188208538Sraj{ 189235529Skientzle struct fdt_header header; 190208538Sraj int err; 191208538Sraj 192243693Sgonzo COPYOUT(va, &header, sizeof(header)); 193243693Sgonzo err = fdt_check_header(&header); 194243693Sgonzo if (err < 0) { 195243693Sgonzo if (err == -FDT_ERR_BADVERSION) 196243693Sgonzo sprintf(command_errbuf, 197243693Sgonzo "incompatible blob version: %d, should be: %d", 198243693Sgonzo fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION); 199243693Sgonzo 200243693Sgonzo else 201243693Sgonzo sprintf(command_errbuf, "error validating blob: %s", 202243693Sgonzo fdt_strerror(err)); 203243693Sgonzo return (1); 204243693Sgonzo } 205243693Sgonzo 206208538Sraj /* 207243693Sgonzo * Release previous blob 208208538Sraj */ 209243693Sgonzo if (fdtp) 210243693Sgonzo free(fdtp); 211208538Sraj 212235529Skientzle fdtp_size = fdt_totalsize(&header); 213235529Skientzle fdtp = malloc(fdtp_size); 214243693Sgonzo 215235529Skientzle if (fdtp == NULL) { 216235529Skientzle command_errmsg = "can't allocate memory for device tree copy"; 217243693Sgonzo return (1); 218235529Skientzle } 219235529Skientzle 220243693Sgonzo fdtp_va = va; 221243693Sgonzo COPYOUT(va, fdtp, fdtp_size); 222243693Sgonzo debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size); 223208538Sraj 224243693Sgonzo return (0); 225243693Sgonzo} 226243693Sgonzo 227243693Sgonzostatic int 228247045Skientzlefdt_load_dtb_addr(struct fdt_header *header) 229243693Sgonzo{ 230265065Sian int err; 231243693Sgonzo 232247250Skientzle fdtp_size = fdt_totalsize(header); 233265065Sian err = fdt_check_header(header); 234265065Sian if (err < 0) { 235265065Sian sprintf(command_errbuf, "error validating blob: %s", 236265065Sian fdt_strerror(err)); 237265065Sian return (err); 238265065Sian } 239247250Skientzle free(fdtp); 240247250Skientzle if ((fdtp = malloc(fdtp_size)) == NULL) { 241247250Skientzle command_errmsg = "can't allocate memory for device tree copy"; 242247045Skientzle return (1); 243208538Sraj } 244247250Skientzle 245247250Skientzle fdtp_va = 0; // Don't write this back into module or kernel. 246247250Skientzle bcopy(header, fdtp, fdtp_size); 247247250Skientzle return (0); 248247045Skientzle} 249243693Sgonzo 250247045Skientzlestatic int 251247045Skientzlefdt_setup_fdtp() 252247045Skientzle{ 253247045Skientzle struct preloaded_file *bfp; 254247045Skientzle struct fdt_header *hdr; 255247201Skientzle const char *s; 256247201Skientzle char *p; 257247045Skientzle vm_offset_t va; 258247045Skientzle 259247045Skientzle if ((bfp = file_findfile(NULL, "dtb")) != NULL) { 260247045Skientzle printf("Using DTB from loaded file.\n"); 261247045Skientzle return fdt_load_dtb(bfp->f_addr); 262247201Skientzle } 263247045Skientzle 264247201Skientzle if (fdt_to_load != NULL) { 265247201Skientzle printf("Using DTB from memory address 0x%08X.\n", 266247201Skientzle (unsigned int)fdt_to_load); 267247201Skientzle return fdt_load_dtb_addr(fdt_to_load); 268247201Skientzle } 269247201Skientzle 270247045Skientzle s = ub_env_get("fdtaddr"); 271247045Skientzle if (s != NULL && *s != '\0') { 272247045Skientzle hdr = (struct fdt_header *)strtoul(s, &p, 16); 273247045Skientzle if (*p == '\0') { 274247045Skientzle printf("Using DTB provided by U-Boot.\n"); 275247045Skientzle return fdt_load_dtb_addr(hdr); 276247045Skientzle } 277247045Skientzle } 278247045Skientzle 279247045Skientzle if ((va = fdt_find_static_dtb()) != 0) { 280247045Skientzle printf("Using DTB compiled into kernel.\n"); 281247045Skientzle return (fdt_load_dtb(va)); 282247045Skientzle } 283247045Skientzle 284247045Skientzle command_errmsg = "no device tree blob found!"; 285247045Skientzle return (1); 286208538Sraj} 287208538Sraj 288208538Sraj#define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \ 289208538Sraj (cellbuf), (lim), (cellsize), 0); 290208538Sraj 291208538Sraj/* Force using base 16 */ 292208538Sraj#define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \ 293208538Sraj (cellbuf), (lim), (cellsize), 16); 294208538Sraj 295208538Srajstatic int 296208538Sraj_fdt_strtovect(char *str, void *cellbuf, int lim, unsigned char cellsize, 297208538Sraj uint8_t base) 298208538Sraj{ 299208538Sraj char *buf = str; 300208538Sraj char *end = str + strlen(str) - 2; 301208538Sraj uint32_t *u32buf = NULL; 302208538Sraj uint8_t *u8buf = NULL; 303208538Sraj int cnt = 0; 304208538Sraj 305208538Sraj if (cellsize == sizeof(uint32_t)) 306208538Sraj u32buf = (uint32_t *)cellbuf; 307208538Sraj else 308208538Sraj u8buf = (uint8_t *)cellbuf; 309208538Sraj 310208538Sraj if (lim == 0) 311208538Sraj return (0); 312208538Sraj 313208538Sraj while (buf < end) { 314208538Sraj 315208538Sraj /* Skip white whitespace(s)/separators */ 316208538Sraj while (!isxdigit(*buf) && buf < end) 317208538Sraj buf++; 318208538Sraj 319208538Sraj if (u32buf != NULL) 320208538Sraj u32buf[cnt] = 321208538Sraj cpu_to_fdt32((uint32_t)strtol(buf, NULL, base)); 322208538Sraj 323208538Sraj else 324208538Sraj u8buf[cnt] = (uint8_t)strtol(buf, NULL, base); 325208538Sraj 326208538Sraj if (cnt + 1 <= lim - 1) 327208538Sraj cnt++; 328208538Sraj else 329208538Sraj break; 330208538Sraj buf++; 331208538Sraj /* Find another number */ 332208538Sraj while ((isxdigit(*buf) || *buf == 'x') && buf < end) 333208538Sraj buf++; 334208538Sraj } 335208538Sraj return (cnt); 336208538Sraj} 337208538Sraj 338208538Sraj#define TMP_MAX_ETH 8 339208538Sraj 340247250Skientzlestatic void 341208538Srajfixup_ethernet(const char *env, char *ethstr, int *eth_no, int len) 342208538Sraj{ 343208538Sraj char *end, *str; 344208538Sraj uint8_t tmp_addr[6]; 345208538Sraj int i, n; 346208538Sraj 347208538Sraj /* Extract interface number */ 348208538Sraj i = strtol(env + 3, &end, 10); 349208538Sraj if (end == (env + 3)) 350208538Sraj /* 'ethaddr' means interface 0 address */ 351208538Sraj n = 0; 352208538Sraj else 353208538Sraj n = i; 354208538Sraj 355208538Sraj if (n > TMP_MAX_ETH) 356208538Sraj return; 357208538Sraj 358208538Sraj str = ub_env_get(env); 359208538Sraj 360208538Sraj /* Convert macaddr string into a vector of uints */ 361208538Sraj fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t)); 362208538Sraj if (n != 0) { 363208538Sraj i = strlen(env) - 7; 364208538Sraj strncpy(ethstr + 8, env + 3, i); 365208538Sraj } 366208538Sraj /* Set actual property to a value from vect */ 367208538Sraj fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr), 368208538Sraj "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t)); 369208538Sraj 370208538Sraj /* Clear ethernet..XXXX.. string */ 371208538Sraj bzero(ethstr + 8, len - 8); 372208538Sraj 373208538Sraj if (n + 1 > *eth_no) 374208538Sraj *eth_no = n + 1; 375208538Sraj} 376208538Sraj 377247250Skientzlestatic void 378208538Srajfixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq) 379208538Sraj{ 380208538Sraj int lo, o = 0, o2, maxo = 0, depth; 381208538Sraj const uint32_t zero = 0; 382208538Sraj 383208538Sraj /* We want to modify every subnode of /cpus */ 384208538Sraj o = fdt_path_offset(fdtp, "/cpus"); 385235261Skientzle if (o < 0) 386235261Skientzle return; 387208538Sraj 388208538Sraj /* maxo should contain offset of node next to /cpus */ 389208538Sraj depth = 0; 390208538Sraj maxo = o; 391208538Sraj while (depth != -1) 392208538Sraj maxo = fdt_next_node(fdtp, maxo, &depth); 393208538Sraj 394208538Sraj /* Find CPU frequency properties */ 395208538Sraj o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency", 396208538Sraj &zero, sizeof(uint32_t)); 397208538Sraj 398208538Sraj o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero, 399208538Sraj sizeof(uint32_t)); 400208538Sraj 401208538Sraj lo = MIN(o, o2); 402208538Sraj 403208538Sraj while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) { 404208538Sraj 405208538Sraj o = fdt_node_offset_by_prop_value(fdtp, lo, 406208538Sraj "clock-frequency", &zero, sizeof(uint32_t)); 407208538Sraj 408208538Sraj o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency", 409208538Sraj &zero, sizeof(uint32_t)); 410208538Sraj 411208538Sraj /* We're only interested in /cpus subnode(s) */ 412208538Sraj if (lo > maxo) 413208538Sraj break; 414208538Sraj 415208538Sraj fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency", 416208538Sraj (uint32_t)cpufreq); 417208538Sraj 418208538Sraj fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency", 419208538Sraj (uint32_t)busfreq); 420208538Sraj 421208538Sraj lo = MIN(o, o2); 422208538Sraj } 423208538Sraj} 424208538Sraj 425247250Skientzlestatic int 426208538Srajfdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells) 427208538Sraj{ 428208538Sraj int cells_in_tuple, i, tuples, tuple_size; 429208538Sraj uint32_t cur_start, cur_size; 430208538Sraj 431208538Sraj cells_in_tuple = (addr_cells + size_cells); 432208538Sraj tuple_size = cells_in_tuple * sizeof(uint32_t); 433208538Sraj tuples = len / tuple_size; 434208538Sraj if (tuples == 0) 435208538Sraj return (EINVAL); 436208538Sraj 437208538Sraj for (i = 0; i < tuples; i++) { 438208538Sraj if (addr_cells == 2) 439208538Sraj cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]); 440208538Sraj else 441208538Sraj cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]); 442208538Sraj 443208538Sraj if (size_cells == 2) 444208538Sraj cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]); 445208538Sraj else 446208538Sraj cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]); 447208538Sraj 448208538Sraj if (cur_size == 0) 449208538Sraj return (EINVAL); 450208538Sraj 451208538Sraj debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n", 452208538Sraj i, cur_start, cur_size); 453208538Sraj } 454208538Sraj return (0); 455208538Sraj} 456208538Sraj 457247250Skientzlestatic void 458208538Srajfixup_memory(struct sys_info *si) 459208538Sraj{ 460208538Sraj struct mem_region *curmr; 461208538Sraj uint32_t addr_cells, size_cells; 462208538Sraj uint32_t *addr_cellsp, *reg, *size_cellsp; 463208538Sraj int err, i, len, memory, realmrno, root; 464208538Sraj uint8_t *buf, *sb; 465243693Sgonzo uint64_t rstart, rsize; 466243693Sgonzo int reserved; 467208538Sraj 468208538Sraj root = fdt_path_offset(fdtp, "/"); 469208538Sraj if (root < 0) { 470208538Sraj sprintf(command_errbuf, "Could not find root node !"); 471208538Sraj return; 472208538Sraj } 473208538Sraj 474208538Sraj memory = fdt_path_offset(fdtp, "/memory"); 475208538Sraj if (memory <= 0) { 476208538Sraj /* Create proper '/memory' node. */ 477208538Sraj memory = fdt_add_subnode(fdtp, root, "memory"); 478208538Sraj if (memory <= 0) { 479208538Sraj sprintf(command_errbuf, "Could not fixup '/memory' " 480208538Sraj "node, error code : %d!\n", memory); 481208538Sraj return; 482208538Sraj } 483208538Sraj 484208538Sraj err = fdt_setprop(fdtp, memory, "device_type", "memory", 485208538Sraj sizeof("memory")); 486208538Sraj 487208538Sraj if (err < 0) 488208538Sraj return; 489208538Sraj } 490208538Sraj 491208538Sraj addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells", 492208538Sraj NULL); 493208538Sraj size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL); 494208538Sraj 495208538Sraj if (addr_cellsp == NULL || size_cellsp == NULL) { 496208538Sraj sprintf(command_errbuf, "Could not fixup '/memory' node : " 497208538Sraj "%s %s property not found in root node!\n", 498208538Sraj (!addr_cellsp) ? "#address-cells" : "", 499208538Sraj (!size_cellsp) ? "#size-cells" : ""); 500208538Sraj return; 501208538Sraj } 502208538Sraj 503208538Sraj addr_cells = fdt32_to_cpu(*addr_cellsp); 504208538Sraj size_cells = fdt32_to_cpu(*size_cellsp); 505208538Sraj 506243693Sgonzo /* 507243693Sgonzo * Convert memreserve data to memreserve property 508243693Sgonzo * Check if property already exists 509243693Sgonzo */ 510243693Sgonzo reserved = fdt_num_mem_rsv(fdtp); 511243693Sgonzo if (reserved && 512243693Sgonzo (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) { 513243693Sgonzo len = (addr_cells + size_cells) * reserved * sizeof(uint32_t); 514243693Sgonzo sb = buf = (uint8_t *)malloc(len); 515243693Sgonzo if (!buf) 516243693Sgonzo return; 517243693Sgonzo 518243693Sgonzo bzero(buf, len); 519243693Sgonzo 520243693Sgonzo for (i = 0; i < reserved; i++) { 521243693Sgonzo curmr = &si->mr[i]; 522243693Sgonzo if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize)) 523243693Sgonzo break; 524243693Sgonzo if (rsize) { 525243693Sgonzo /* Ensure endianess, and put cells into a buffer */ 526243693Sgonzo if (addr_cells == 2) 527243693Sgonzo *(uint64_t *)buf = 528243693Sgonzo cpu_to_fdt64(rstart); 529243693Sgonzo else 530243693Sgonzo *(uint32_t *)buf = 531243693Sgonzo cpu_to_fdt32(rstart); 532243693Sgonzo 533243693Sgonzo buf += sizeof(uint32_t) * addr_cells; 534243693Sgonzo if (size_cells == 2) 535243693Sgonzo *(uint64_t *)buf = 536243693Sgonzo cpu_to_fdt64(rsize); 537243693Sgonzo else 538243693Sgonzo *(uint32_t *)buf = 539243693Sgonzo cpu_to_fdt32(rsize); 540243693Sgonzo 541243693Sgonzo buf += sizeof(uint32_t) * size_cells; 542243693Sgonzo } 543243693Sgonzo } 544243693Sgonzo 545243693Sgonzo /* Set property */ 546243693Sgonzo if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0) 547243693Sgonzo printf("Could not fixup 'memreserve' property.\n"); 548243693Sgonzo 549243693Sgonzo free(sb); 550243693Sgonzo } 551243693Sgonzo 552208538Sraj /* Count valid memory regions entries in sysinfo. */ 553208538Sraj realmrno = si->mr_no; 554208538Sraj for (i = 0; i < si->mr_no; i++) 555208538Sraj if (si->mr[i].start == 0 && si->mr[i].size == 0) 556208538Sraj realmrno--; 557208538Sraj 558208538Sraj if (realmrno == 0) { 559208538Sraj sprintf(command_errbuf, "Could not fixup '/memory' node : " 560208538Sraj "sysinfo doesn't contain valid memory regions info!\n"); 561208538Sraj return; 562208538Sraj } 563208538Sraj 564208538Sraj if ((reg = (uint32_t *)fdt_getprop(fdtp, memory, "reg", 565208538Sraj &len)) != NULL) { 566208538Sraj 567208538Sraj if (fdt_reg_valid(reg, len, addr_cells, size_cells) == 0) 568208538Sraj /* 569208538Sraj * Do not apply fixup if existing 'reg' property 570208538Sraj * seems to be valid. 571208538Sraj */ 572208538Sraj return; 573208538Sraj } 574208538Sraj 575208538Sraj len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t); 576208538Sraj sb = buf = (uint8_t *)malloc(len); 577208538Sraj if (!buf) 578208538Sraj return; 579208538Sraj 580208538Sraj bzero(buf, len); 581208538Sraj 582208538Sraj for (i = 0; i < si->mr_no; i++) { 583208538Sraj curmr = &si->mr[i]; 584208538Sraj if (curmr->size != 0) { 585208538Sraj /* Ensure endianess, and put cells into a buffer */ 586208538Sraj if (addr_cells == 2) 587208538Sraj *(uint64_t *)buf = 588208538Sraj cpu_to_fdt64(curmr->start); 589208538Sraj else 590208538Sraj *(uint32_t *)buf = 591208538Sraj cpu_to_fdt32(curmr->start); 592208538Sraj 593208538Sraj buf += sizeof(uint32_t) * addr_cells; 594208538Sraj if (size_cells == 2) 595208538Sraj *(uint64_t *)buf = 596208538Sraj cpu_to_fdt64(curmr->size); 597208538Sraj else 598208538Sraj *(uint32_t *)buf = 599208538Sraj cpu_to_fdt32(curmr->size); 600208538Sraj 601208538Sraj buf += sizeof(uint32_t) * size_cells; 602208538Sraj } 603208538Sraj } 604208538Sraj 605208538Sraj /* Set property */ 606208538Sraj if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0) 607208538Sraj sprintf(command_errbuf, "Could not fixup '/memory' node.\n"); 608243693Sgonzo 609243693Sgonzo free(sb); 610208538Sraj} 611208538Sraj 612247250Skientzlestatic void 613208538Srajfixup_stdout(const char *env) 614208538Sraj{ 615208538Sraj const char *str; 616208538Sraj char *ptr; 617208538Sraj int serialno; 618208538Sraj int len, no, sero; 619208538Sraj const struct fdt_property *prop; 620208538Sraj char *tmp[10]; 621208538Sraj 622208538Sraj str = ub_env_get(env); 623208538Sraj ptr = (char *)str + strlen(str) - 1; 624208538Sraj while (ptr > str && isdigit(*(str - 1))) 625208538Sraj str--; 626208538Sraj 627208538Sraj if (ptr == str) 628208538Sraj return; 629208538Sraj 630208538Sraj serialno = (int)strtol(ptr, NULL, 0); 631208538Sraj no = fdt_path_offset(fdtp, "/chosen"); 632208538Sraj if (no < 0) 633208538Sraj return; 634208538Sraj 635208538Sraj prop = fdt_get_property(fdtp, no, "stdout", &len); 636208538Sraj 637208538Sraj /* If /chosen/stdout does not extist, create it */ 638208538Sraj if (prop == NULL || (prop != NULL && len == 0)) { 639208538Sraj 640208538Sraj bzero(tmp, 10 * sizeof(char)); 641208538Sraj strcpy((char *)&tmp, "serial"); 642208538Sraj if (strlen(ptr) > 3) 643208538Sraj /* Serial number too long */ 644208538Sraj return; 645208538Sraj 646208538Sraj strncpy((char *)tmp + 6, ptr, 3); 647208538Sraj sero = fdt_path_offset(fdtp, (const char *)tmp); 648208538Sraj if (sero < 0) 649208538Sraj /* 650208538Sraj * If serial device we're trying to assign 651208538Sraj * stdout to doesn't exist in DT -- return. 652208538Sraj */ 653208538Sraj return; 654208538Sraj 655208538Sraj fdt_setprop(fdtp, no, "stdout", &tmp, 656208538Sraj strlen((char *)&tmp) + 1); 657208538Sraj fdt_setprop(fdtp, no, "stdin", &tmp, 658208538Sraj strlen((char *)&tmp) + 1); 659208538Sraj } 660208538Sraj} 661208538Sraj 662233230Sraj/* 663233230Sraj * Locate the blob, fix it up and return its location. 664233230Sraj */ 665247250Skientzlestatic int 666208538Srajfdt_fixup(void) 667208538Sraj{ 668208538Sraj const char *env; 669208538Sraj char *ethstr; 670265065Sian int chosen, eth_no, len; 671208538Sraj struct sys_info *si; 672208538Sraj 673208538Sraj env = NULL; 674208538Sraj eth_no = 0; 675208538Sraj ethstr = NULL; 676208538Sraj len = 0; 677208538Sraj 678265065Sian if (fdtp == NULL && fdt_setup_fdtp() != 0) 679265065Sian return (0); 680208538Sraj 681208538Sraj /* Create /chosen node (if not exists) */ 682208538Sraj if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) == 683208538Sraj -FDT_ERR_NOTFOUND) 684208538Sraj chosen = fdt_add_subnode(fdtp, 0, "chosen"); 685208538Sraj 686208538Sraj /* Value assigned to fixup-applied does not matter. */ 687208538Sraj if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL)) 688247250Skientzle return (1); 689208538Sraj 690208538Sraj /* Acquire sys_info */ 691208538Sraj si = ub_get_sys_info(); 692208538Sraj 693208538Sraj while ((env = ub_env_enum(env)) != NULL) { 694208538Sraj if (strncmp(env, "eth", 3) == 0 && 695208538Sraj strncmp(env + (strlen(env) - 4), "addr", 4) == 0) { 696208538Sraj /* 697208538Sraj * Handle Ethernet addrs: parse uboot env eth%daddr 698208538Sraj */ 699208538Sraj 700208538Sraj if (!eth_no) { 701208538Sraj /* 702208538Sraj * Check how many chars we will need to store 703208538Sraj * maximal eth iface number. 704208538Sraj */ 705208538Sraj len = strlen(STRINGIFY(TMP_MAX_ETH)) + 706208538Sraj strlen("ethernet"); 707208538Sraj 708208538Sraj /* 709208538Sraj * Reserve mem for string "ethernet" and len 710208538Sraj * chars for iface no. 711208538Sraj */ 712208538Sraj ethstr = (char *)malloc(len * sizeof(char)); 713208538Sraj bzero(ethstr, len * sizeof(char)); 714208538Sraj strcpy(ethstr, "ethernet0"); 715208538Sraj } 716208538Sraj 717208538Sraj /* Modify blob */ 718208538Sraj fixup_ethernet(env, ethstr, ð_no, len); 719208538Sraj 720208538Sraj } else if (strcmp(env, "consoledev") == 0) 721208538Sraj fixup_stdout(env); 722208538Sraj } 723208538Sraj 724208538Sraj /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */ 725208538Sraj fixup_cpubusfreqs(si->clk_cpu, si->clk_bus); 726208538Sraj 727208538Sraj /* Fixup memory regions */ 728208538Sraj fixup_memory(si); 729208538Sraj 730208538Sraj fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0); 731247250Skientzle return (1); 732208538Sraj} 733208538Sraj 734243693Sgonzo/* 735247250Skientzle * Copy DTB blob to specified location and return size 736243693Sgonzo */ 737208538Srajint 738243693Sgonzofdt_copy(vm_offset_t va) 739243693Sgonzo{ 740243693Sgonzo int err; 741243693Sgonzo 742243693Sgonzo if (fdtp == NULL) { 743243693Sgonzo err = fdt_setup_fdtp(); 744243693Sgonzo if (err) { 745265065Sian printf("No valid device tree blob found!\n"); 746243693Sgonzo return (0); 747243693Sgonzo } 748243693Sgonzo } 749243693Sgonzo 750243693Sgonzo if (fdt_fixup() == 0) 751243693Sgonzo return (0); 752243693Sgonzo 753247250Skientzle if (fdtp_va != 0) { 754247250Skientzle /* Overwrite the FDT with the fixed version. */ 755247250Skientzle /* XXX Is this really appropriate? */ 756247250Skientzle COPYIN(fdtp, fdtp_va, fdtp_size); 757247250Skientzle } 758243693Sgonzo COPYIN(fdtp, va, fdtp_size); 759243693Sgonzo return (fdtp_size); 760243693Sgonzo} 761243693Sgonzo 762243693Sgonzo 763243693Sgonzo 764243693Sgonzoint 765208538Srajcommand_fdt_internal(int argc, char *argv[]) 766208538Sraj{ 767208538Sraj cmdf_t *cmdh; 768243693Sgonzo int flags; 769208538Sraj char *cmd; 770208538Sraj int i, err; 771208538Sraj 772208538Sraj if (argc < 2) { 773208538Sraj command_errmsg = "usage is 'fdt <command> [<args>]"; 774208538Sraj return (CMD_ERROR); 775208538Sraj } 776208538Sraj 777208538Sraj /* 778208538Sraj * Validate fdt <command>. 779208538Sraj */ 780208538Sraj cmd = strdup(argv[1]); 781208538Sraj i = 0; 782208538Sraj cmdh = NULL; 783208538Sraj while (!(commands[i].name == NULL)) { 784208538Sraj if (strcmp(cmd, commands[i].name) == 0) { 785208538Sraj /* found it */ 786208538Sraj cmdh = commands[i].handler; 787243693Sgonzo flags = commands[i].flags; 788208538Sraj break; 789208538Sraj } 790208538Sraj i++; 791208538Sraj } 792208538Sraj if (cmdh == NULL) { 793208538Sraj command_errmsg = "unknown command"; 794208538Sraj return (CMD_ERROR); 795208538Sraj } 796208538Sraj 797243693Sgonzo if (flags & CMD_REQUIRES_BLOB) { 798243693Sgonzo /* 799243693Sgonzo * Check if uboot env vars were parsed already. If not, do it now. 800243693Sgonzo */ 801243693Sgonzo if (fdt_fixup() == 0) 802243693Sgonzo return (CMD_ERROR); 803243693Sgonzo } 804243693Sgonzo 805208538Sraj /* 806208538Sraj * Call command handler. 807208538Sraj */ 808208538Sraj err = (*cmdh)(argc, argv); 809208538Sraj 810208538Sraj return (err); 811208538Sraj} 812208538Sraj 813208538Srajstatic int 814243693Sgonzofdt_cmd_addr(int argc, char *argv[]) 815243693Sgonzo{ 816247201Skientzle struct preloaded_file *fp; 817247045Skientzle struct fdt_header *hdr; 818247201Skientzle const char *addr; 819247201Skientzle char *cp; 820243693Sgonzo 821247201Skientzle fdt_to_load = NULL; 822247201Skientzle 823243693Sgonzo if (argc > 2) 824243693Sgonzo addr = argv[2]; 825243693Sgonzo else { 826243693Sgonzo sprintf(command_errbuf, "no address specified"); 827243693Sgonzo return (CMD_ERROR); 828243693Sgonzo } 829243693Sgonzo 830247201Skientzle hdr = (struct fdt_header *)strtoul(addr, &cp, 16); 831243693Sgonzo if (cp == addr) { 832243693Sgonzo sprintf(command_errbuf, "Invalid address: %s", addr); 833243693Sgonzo return (CMD_ERROR); 834243693Sgonzo } 835243693Sgonzo 836247201Skientzle while ((fp = file_findfile(NULL, "dtb")) != NULL) { 837247201Skientzle file_discard(fp); 838247201Skientzle } 839243693Sgonzo 840247201Skientzle fdt_to_load = hdr; 841243693Sgonzo return (CMD_OK); 842243693Sgonzo} 843243693Sgonzo 844243693Sgonzostatic int 845208538Srajfdt_cmd_cd(int argc, char *argv[]) 846208538Sraj{ 847208538Sraj char *path; 848208538Sraj char tmp[FDT_CWD_LEN]; 849208538Sraj int len, o; 850208538Sraj 851208538Sraj path = (argc > 2) ? argv[2] : "/"; 852208538Sraj 853208538Sraj if (path[0] == '/') { 854208538Sraj len = strlen(path); 855208538Sraj if (len >= FDT_CWD_LEN) 856208538Sraj goto fail; 857208538Sraj } else { 858208538Sraj /* Handle path specification relative to cwd */ 859208538Sraj len = strlen(cwd) + strlen(path) + 1; 860208538Sraj if (len >= FDT_CWD_LEN) 861208538Sraj goto fail; 862208538Sraj 863208538Sraj strcpy(tmp, cwd); 864208538Sraj strcat(tmp, "/"); 865208538Sraj strcat(tmp, path); 866208538Sraj path = tmp; 867208538Sraj } 868208538Sraj 869208538Sraj o = fdt_path_offset(fdtp, path); 870208538Sraj if (o < 0) { 871208538Sraj sprintf(command_errbuf, "could not find node: '%s'", path); 872208538Sraj return (CMD_ERROR); 873208538Sraj } 874208538Sraj 875208538Sraj strcpy(cwd, path); 876208538Sraj return (CMD_OK); 877208538Sraj 878208538Srajfail: 879208538Sraj sprintf(command_errbuf, "path too long: %d, max allowed: %d", 880208538Sraj len, FDT_CWD_LEN - 1); 881208538Sraj return (CMD_ERROR); 882208538Sraj} 883208538Sraj 884208538Srajstatic int 885208538Srajfdt_cmd_hdr(int argc __unused, char *argv[] __unused) 886208538Sraj{ 887208538Sraj char line[80]; 888208538Sraj int ver; 889208538Sraj 890208538Sraj if (fdtp == NULL) { 891208538Sraj command_errmsg = "no device tree blob pointer?!"; 892208538Sraj return (CMD_ERROR); 893208538Sraj } 894208538Sraj 895208538Sraj ver = fdt_version(fdtp); 896208538Sraj pager_open(); 897208538Sraj sprintf(line, "\nFlattened device tree header (%p):\n", fdtp); 898208538Sraj pager_output(line); 899208538Sraj sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp)); 900208538Sraj pager_output(line); 901208538Sraj sprintf(line, " size = %d\n", fdt_totalsize(fdtp)); 902208538Sraj pager_output(line); 903208538Sraj sprintf(line, " off_dt_struct = 0x%08x\n", 904208538Sraj fdt_off_dt_struct(fdtp)); 905208538Sraj pager_output(line); 906208538Sraj sprintf(line, " off_dt_strings = 0x%08x\n", 907208538Sraj fdt_off_dt_strings(fdtp)); 908208538Sraj pager_output(line); 909208538Sraj sprintf(line, " off_mem_rsvmap = 0x%08x\n", 910208538Sraj fdt_off_mem_rsvmap(fdtp)); 911208538Sraj pager_output(line); 912208538Sraj sprintf(line, " version = %d\n", ver); 913208538Sraj pager_output(line); 914208538Sraj sprintf(line, " last compatible version = %d\n", 915208538Sraj fdt_last_comp_version(fdtp)); 916208538Sraj pager_output(line); 917208538Sraj if (ver >= 2) { 918208538Sraj sprintf(line, " boot_cpuid = %d\n", 919208538Sraj fdt_boot_cpuid_phys(fdtp)); 920208538Sraj pager_output(line); 921208538Sraj } 922208538Sraj if (ver >= 3) { 923208538Sraj sprintf(line, " size_dt_strings = %d\n", 924208538Sraj fdt_size_dt_strings(fdtp)); 925208538Sraj pager_output(line); 926208538Sraj } 927208538Sraj if (ver >= 17) { 928208538Sraj sprintf(line, " size_dt_struct = %d\n", 929208538Sraj fdt_size_dt_struct(fdtp)); 930208538Sraj pager_output(line); 931208538Sraj } 932208538Sraj pager_close(); 933208538Sraj 934208538Sraj return (CMD_OK); 935208538Sraj} 936208538Sraj 937208538Srajstatic int 938208538Srajfdt_cmd_ls(int argc, char *argv[]) 939208538Sraj{ 940208538Sraj const char *prevname[FDT_MAX_DEPTH] = { NULL }; 941208538Sraj const char *name; 942208538Sraj char *path; 943208538Sraj int i, o, depth, len; 944208538Sraj 945208538Sraj path = (argc > 2) ? argv[2] : NULL; 946208538Sraj if (path == NULL) 947208538Sraj path = cwd; 948208538Sraj 949208538Sraj o = fdt_path_offset(fdtp, path); 950208538Sraj if (o < 0) { 951208538Sraj sprintf(command_errbuf, "could not find node: '%s'", path); 952208538Sraj return (CMD_ERROR); 953208538Sraj } 954208538Sraj 955208538Sraj for (depth = 0; 956208538Sraj (o >= 0) && (depth >= 0); 957208538Sraj o = fdt_next_node(fdtp, o, &depth)) { 958208538Sraj 959208538Sraj name = fdt_get_name(fdtp, o, &len); 960208538Sraj 961208538Sraj if (depth > FDT_MAX_DEPTH) { 962208538Sraj printf("max depth exceeded: %d\n", depth); 963208538Sraj continue; 964208538Sraj } 965208538Sraj 966208538Sraj prevname[depth] = name; 967208538Sraj 968208538Sraj /* Skip root (i = 1) when printing devices */ 969208538Sraj for (i = 1; i <= depth; i++) { 970208538Sraj if (prevname[i] == NULL) 971208538Sraj break; 972208538Sraj 973208538Sraj if (strcmp(cwd, "/") == 0) 974208538Sraj printf("/"); 975208538Sraj printf("%s", prevname[i]); 976208538Sraj } 977208538Sraj printf("\n"); 978208538Sraj } 979208538Sraj 980208538Sraj return (CMD_OK); 981208538Sraj} 982208538Sraj 983208538Srajstatic __inline int 984208538Srajisprint(int c) 985208538Sraj{ 986208538Sraj 987208538Sraj return (c >= ' ' && c <= 0x7e); 988208538Sraj} 989208538Sraj 990208538Srajstatic int 991208538Srajfdt_isprint(const void *data, int len, int *count) 992208538Sraj{ 993208538Sraj const char *d; 994208538Sraj char ch; 995208538Sraj int yesno, i; 996208538Sraj 997208538Sraj if (len == 0) 998208538Sraj return (0); 999208538Sraj 1000208538Sraj d = (const char *)data; 1001208538Sraj if (d[len - 1] != '\0') 1002208538Sraj return (0); 1003208538Sraj 1004208538Sraj *count = 0; 1005208538Sraj yesno = 1; 1006208538Sraj for (i = 0; i < len; i++) { 1007208538Sraj ch = *(d + i); 1008208538Sraj if (isprint(ch) || (ch == '\0' && i > 0)) { 1009208538Sraj /* Count strings */ 1010208538Sraj if (ch == '\0') 1011208538Sraj (*count)++; 1012208538Sraj continue; 1013208538Sraj } 1014208538Sraj 1015208538Sraj yesno = 0; 1016208538Sraj break; 1017208538Sraj } 1018208538Sraj 1019208538Sraj return (yesno); 1020208538Sraj} 1021208538Sraj 1022208538Srajstatic int 1023208538Srajfdt_data_str(const void *data, int len, int count, char **buf) 1024208538Sraj{ 1025233323Sraj char *b, *tmp; 1026208538Sraj const char *d; 1027233323Sraj int buf_len, i, l; 1028208538Sraj 1029208538Sraj /* 1030208538Sraj * Calculate the length for the string and allocate memory. 1031208538Sraj * 1032233323Sraj * Note that 'len' already includes at least one terminator. 1033208538Sraj */ 1034233323Sraj buf_len = len; 1035208538Sraj if (count > 1) { 1036208538Sraj /* 1037208538Sraj * Each token had already a terminator buried in 'len', but we 1038208538Sraj * only need one eventually, don't count space for these. 1039208538Sraj */ 1040233323Sraj buf_len -= count - 1; 1041208538Sraj 1042208538Sraj /* Each consecutive token requires a ", " separator. */ 1043233323Sraj buf_len += count * 2; 1044208538Sraj } 1045208538Sraj 1046233323Sraj /* Add some space for surrounding double quotes. */ 1047233323Sraj buf_len += count * 2; 1048233323Sraj 1049233323Sraj /* Note that string being put in 'tmp' may be as big as 'buf_len'. */ 1050233323Sraj b = (char *)malloc(buf_len); 1051233323Sraj tmp = (char *)malloc(buf_len); 1052208538Sraj if (b == NULL) 1053233323Sraj goto error; 1054233323Sraj 1055233323Sraj if (tmp == NULL) { 1056233323Sraj free(b); 1057233323Sraj goto error; 1058233323Sraj } 1059233323Sraj 1060208538Sraj b[0] = '\0'; 1061208538Sraj 1062208538Sraj /* 1063208538Sraj * Now that we have space, format the string. 1064208538Sraj */ 1065208538Sraj i = 0; 1066208538Sraj do { 1067208538Sraj d = (const char *)data + i; 1068208538Sraj l = strlen(d) + 1; 1069208538Sraj 1070208538Sraj sprintf(tmp, "\"%s\"%s", d, 1071208538Sraj (i + l) < len ? ", " : ""); 1072208538Sraj strcat(b, tmp); 1073208538Sraj 1074208538Sraj i += l; 1075208538Sraj 1076208538Sraj } while (i < len); 1077208538Sraj *buf = b; 1078208538Sraj 1079233323Sraj free(tmp); 1080233323Sraj 1081208538Sraj return (0); 1082233323Srajerror: 1083233323Sraj return (1); 1084208538Sraj} 1085208538Sraj 1086208538Srajstatic int 1087208538Srajfdt_data_cell(const void *data, int len, char **buf) 1088208538Sraj{ 1089233323Sraj char *b, *tmp; 1090208538Sraj const uint32_t *c; 1091208538Sraj int count, i, l; 1092208538Sraj 1093208538Sraj /* Number of cells */ 1094208538Sraj count = len / 4; 1095208538Sraj 1096208538Sraj /* 1097208538Sraj * Calculate the length for the string and allocate memory. 1098208538Sraj */ 1099208538Sraj 1100208538Sraj /* Each byte translates to 2 output characters */ 1101208538Sraj l = len * 2; 1102208538Sraj if (count > 1) { 1103208538Sraj /* Each consecutive cell requires a " " separator. */ 1104208538Sraj l += (count - 1) * 1; 1105208538Sraj } 1106208538Sraj /* Each cell will have a "0x" prefix */ 1107208538Sraj l += count * 2; 1108208538Sraj /* Space for surrounding <> and terminator */ 1109208538Sraj l += 3; 1110208538Sraj 1111208538Sraj b = (char *)malloc(l); 1112233323Sraj tmp = (char *)malloc(l); 1113208538Sraj if (b == NULL) 1114233323Sraj goto error; 1115208538Sraj 1116233323Sraj if (tmp == NULL) { 1117233323Sraj free(b); 1118233323Sraj goto error; 1119233323Sraj } 1120233323Sraj 1121208538Sraj b[0] = '\0'; 1122208538Sraj strcat(b, "<"); 1123208538Sraj 1124208538Sraj for (i = 0; i < len; i += 4) { 1125208538Sraj c = (const uint32_t *)((const uint8_t *)data + i); 1126208538Sraj sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c), 1127208538Sraj i < (len - 4) ? " " : ""); 1128208538Sraj strcat(b, tmp); 1129208538Sraj } 1130208538Sraj strcat(b, ">"); 1131208538Sraj *buf = b; 1132208538Sraj 1133233323Sraj free(tmp); 1134233323Sraj 1135208538Sraj return (0); 1136233323Srajerror: 1137233323Sraj return (1); 1138208538Sraj} 1139208538Sraj 1140208538Srajstatic int 1141208538Srajfdt_data_bytes(const void *data, int len, char **buf) 1142208538Sraj{ 1143233323Sraj char *b, *tmp; 1144208538Sraj const char *d; 1145208538Sraj int i, l; 1146208538Sraj 1147208538Sraj /* 1148208538Sraj * Calculate the length for the string and allocate memory. 1149208538Sraj */ 1150208538Sraj 1151208538Sraj /* Each byte translates to 2 output characters */ 1152208538Sraj l = len * 2; 1153208538Sraj if (len > 1) 1154208538Sraj /* Each consecutive byte requires a " " separator. */ 1155208538Sraj l += (len - 1) * 1; 1156208538Sraj /* Each byte will have a "0x" prefix */ 1157208538Sraj l += len * 2; 1158208538Sraj /* Space for surrounding [] and terminator. */ 1159208538Sraj l += 3; 1160208538Sraj 1161208538Sraj b = (char *)malloc(l); 1162233323Sraj tmp = (char *)malloc(l); 1163208538Sraj if (b == NULL) 1164233323Sraj goto error; 1165208538Sraj 1166233323Sraj if (tmp == NULL) { 1167233323Sraj free(b); 1168233323Sraj goto error; 1169233323Sraj } 1170233323Sraj 1171208538Sraj b[0] = '\0'; 1172208538Sraj strcat(b, "["); 1173208538Sraj 1174208538Sraj for (i = 0, d = data; i < len; i++) { 1175208538Sraj sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : ""); 1176208538Sraj strcat(b, tmp); 1177208538Sraj } 1178208538Sraj strcat(b, "]"); 1179208538Sraj *buf = b; 1180208538Sraj 1181233323Sraj free(tmp); 1182233323Sraj 1183208538Sraj return (0); 1184233323Srajerror: 1185233323Sraj return (1); 1186208538Sraj} 1187208538Sraj 1188208538Srajstatic int 1189208538Srajfdt_data_fmt(const void *data, int len, char **buf) 1190208538Sraj{ 1191208538Sraj int count; 1192208538Sraj 1193208538Sraj if (len == 0) { 1194208538Sraj *buf = NULL; 1195208538Sraj return (1); 1196208538Sraj } 1197208538Sraj 1198208538Sraj if (fdt_isprint(data, len, &count)) 1199208538Sraj return (fdt_data_str(data, len, count, buf)); 1200208538Sraj 1201208538Sraj else if ((len % 4) == 0) 1202208538Sraj return (fdt_data_cell(data, len, buf)); 1203208538Sraj 1204208538Sraj else 1205208538Sraj return (fdt_data_bytes(data, len, buf)); 1206208538Sraj} 1207208538Sraj 1208208538Srajstatic int 1209208538Srajfdt_prop(int offset) 1210208538Sraj{ 1211208538Sraj char *line, *buf; 1212208538Sraj const struct fdt_property *prop; 1213208538Sraj const char *name; 1214208538Sraj const void *data; 1215208538Sraj int len, rv; 1216208538Sraj 1217208538Sraj line = NULL; 1218208538Sraj prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop)); 1219208538Sraj if (prop == NULL) 1220208538Sraj return (1); 1221208538Sraj 1222208538Sraj name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff)); 1223208538Sraj len = fdt32_to_cpu(prop->len); 1224208538Sraj 1225208538Sraj rv = 0; 1226208538Sraj buf = NULL; 1227208538Sraj if (len == 0) { 1228208538Sraj /* Property without value */ 1229208538Sraj line = (char *)malloc(strlen(name) + 2); 1230208538Sraj if (line == NULL) { 1231208538Sraj rv = 2; 1232208538Sraj goto out2; 1233208538Sraj } 1234208538Sraj sprintf(line, "%s\n", name); 1235208538Sraj goto out1; 1236208538Sraj } 1237208538Sraj 1238208538Sraj /* 1239208538Sraj * Process property with value 1240208538Sraj */ 1241208538Sraj data = prop->data; 1242208538Sraj 1243208538Sraj if (fdt_data_fmt(data, len, &buf) != 0) { 1244208538Sraj rv = 3; 1245208538Sraj goto out2; 1246208538Sraj } 1247208538Sraj 1248208538Sraj line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) + 1249208538Sraj strlen(buf) + 2); 1250208538Sraj if (line == NULL) { 1251208538Sraj sprintf(command_errbuf, "could not allocate space for string"); 1252208538Sraj rv = 4; 1253208538Sraj goto out2; 1254208538Sraj } 1255208538Sraj 1256208538Sraj sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf); 1257208538Sraj 1258208538Srajout1: 1259208538Sraj pager_open(); 1260208538Sraj pager_output(line); 1261208538Sraj pager_close(); 1262208538Sraj 1263208538Srajout2: 1264208538Sraj if (buf) 1265208538Sraj free(buf); 1266208538Sraj 1267208538Sraj if (line) 1268208538Sraj free(line); 1269208538Sraj 1270208538Sraj return (rv); 1271208538Sraj} 1272208538Sraj 1273208538Srajstatic int 1274208538Srajfdt_modprop(int nodeoff, char *propname, void *value, char mode) 1275208538Sraj{ 1276208538Sraj uint32_t cells[100]; 1277208538Sraj char *buf; 1278208538Sraj int len, rv; 1279208538Sraj const struct fdt_property *p; 1280208538Sraj 1281208538Sraj p = fdt_get_property(fdtp, nodeoff, propname, NULL); 1282208538Sraj 1283208538Sraj if (p != NULL) { 1284208538Sraj if (mode == 1) { 1285208538Sraj /* Adding inexistant value in mode 1 is forbidden */ 1286208538Sraj sprintf(command_errbuf, "property already exists!"); 1287208538Sraj return (CMD_ERROR); 1288208538Sraj } 1289208538Sraj } else if (mode == 0) { 1290208538Sraj sprintf(command_errbuf, "property does not exist!"); 1291208538Sraj return (CMD_ERROR); 1292208538Sraj } 1293208538Sraj len = strlen(value); 1294208538Sraj rv = 0; 1295208538Sraj buf = (char *)value; 1296208538Sraj 1297208538Sraj switch (*buf) { 1298208538Sraj case '&': 1299208538Sraj /* phandles */ 1300208538Sraj break; 1301208538Sraj case '<': 1302208538Sraj /* Data cells */ 1303208538Sraj len = fdt_strtovect(buf, (void *)&cells, 100, 1304208538Sraj sizeof(uint32_t)); 1305208538Sraj 1306208538Sraj rv = fdt_setprop(fdtp, nodeoff, propname, &cells, 1307208538Sraj len * sizeof(uint32_t)); 1308208538Sraj break; 1309208538Sraj case '[': 1310208538Sraj /* Data bytes */ 1311208538Sraj len = fdt_strtovect(buf, (void *)&cells, 100, 1312208538Sraj sizeof(uint8_t)); 1313208538Sraj 1314208538Sraj rv = fdt_setprop(fdtp, nodeoff, propname, &cells, 1315208538Sraj len * sizeof(uint8_t)); 1316208538Sraj break; 1317208538Sraj case '"': 1318208538Sraj default: 1319208538Sraj /* Default -- string */ 1320208538Sraj rv = fdt_setprop_string(fdtp, nodeoff, propname, value); 1321208538Sraj break; 1322208538Sraj } 1323208538Sraj 1324233323Sraj if (rv != 0) { 1325233323Sraj if (rv == -FDT_ERR_NOSPACE) 1326233323Sraj sprintf(command_errbuf, 1327233323Sraj "Device tree blob is too small!\n"); 1328233323Sraj else 1329233323Sraj sprintf(command_errbuf, 1330233323Sraj "Could not add/modify property!\n"); 1331233323Sraj } 1332208538Sraj return (rv); 1333208538Sraj} 1334208538Sraj 1335208538Sraj/* Merge strings from argv into a single string */ 1336208538Srajstatic int 1337208538Srajfdt_merge_strings(int argc, char *argv[], int start, char **buffer) 1338208538Sraj{ 1339208538Sraj char *buf; 1340208538Sraj int i, idx, sz; 1341208538Sraj 1342208538Sraj *buffer = NULL; 1343208538Sraj sz = 0; 1344208538Sraj 1345208538Sraj for (i = start; i < argc; i++) 1346208538Sraj sz += strlen(argv[i]); 1347208538Sraj 1348208538Sraj /* Additional bytes for whitespaces between args */ 1349208538Sraj sz += argc - start; 1350208538Sraj 1351208538Sraj buf = (char *)malloc(sizeof(char) * sz); 1352208538Sraj bzero(buf, sizeof(char) * sz); 1353208538Sraj 1354208538Sraj if (buf == NULL) { 1355208538Sraj sprintf(command_errbuf, "could not allocate space " 1356208538Sraj "for string"); 1357208538Sraj return (1); 1358208538Sraj } 1359208538Sraj 1360208538Sraj idx = 0; 1361208538Sraj for (i = start, idx = 0; i < argc; i++) { 1362208538Sraj strcpy(buf + idx, argv[i]); 1363208538Sraj idx += strlen(argv[i]); 1364208538Sraj buf[idx] = ' '; 1365208538Sraj idx++; 1366208538Sraj } 1367208538Sraj buf[sz - 1] = '\0'; 1368208538Sraj *buffer = buf; 1369208538Sraj return (0); 1370208538Sraj} 1371208538Sraj 1372208538Sraj/* Extract offset and name of node/property from a given path */ 1373208538Srajstatic int 1374208538Srajfdt_extract_nameloc(char **pathp, char **namep, int *nodeoff) 1375208538Sraj{ 1376208538Sraj int o; 1377208538Sraj char *path = *pathp, *name = NULL, *subpath = NULL; 1378208538Sraj 1379208538Sraj subpath = strrchr(path, '/'); 1380208538Sraj if (subpath == NULL) { 1381208538Sraj o = fdt_path_offset(fdtp, cwd); 1382208538Sraj name = path; 1383208538Sraj path = (char *)&cwd; 1384208538Sraj } else { 1385208538Sraj *subpath = '\0'; 1386208538Sraj if (strlen(path) == 0) 1387208538Sraj path = cwd; 1388208538Sraj 1389208538Sraj name = subpath + 1; 1390208538Sraj o = fdt_path_offset(fdtp, path); 1391208538Sraj } 1392208538Sraj 1393208538Sraj if (strlen(name) == 0) { 1394208538Sraj sprintf(command_errbuf, "name not specified"); 1395208538Sraj return (1); 1396208538Sraj } 1397208538Sraj if (o < 0) { 1398208538Sraj sprintf(command_errbuf, "could not find node: '%s'", path); 1399208538Sraj return (1); 1400208538Sraj } 1401208538Sraj *namep = name; 1402208538Sraj *nodeoff = o; 1403208538Sraj *pathp = path; 1404208538Sraj return (0); 1405208538Sraj} 1406208538Sraj 1407208538Srajstatic int 1408208538Srajfdt_cmd_prop(int argc, char *argv[]) 1409208538Sraj{ 1410208538Sraj char *path, *propname, *value; 1411208538Sraj int o, next, depth, rv; 1412208538Sraj uint32_t tag; 1413208538Sraj 1414208538Sraj path = (argc > 2) ? argv[2] : NULL; 1415208538Sraj 1416208538Sraj value = NULL; 1417208538Sraj 1418208538Sraj if (argc > 3) { 1419208538Sraj /* Merge property value strings into one */ 1420208538Sraj if (fdt_merge_strings(argc, argv, 3, &value) != 0) 1421208538Sraj return (CMD_ERROR); 1422208538Sraj } else 1423208538Sraj value = NULL; 1424208538Sraj 1425208538Sraj if (path == NULL) 1426208538Sraj path = cwd; 1427208538Sraj 1428208538Sraj rv = CMD_OK; 1429208538Sraj 1430208538Sraj if (value) { 1431208538Sraj /* If value is specified -- try to modify prop. */ 1432208538Sraj if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1433208538Sraj return (CMD_ERROR); 1434208538Sraj 1435208538Sraj rv = fdt_modprop(o, propname, value, 0); 1436208538Sraj if (rv) 1437208538Sraj return (CMD_ERROR); 1438208538Sraj return (CMD_OK); 1439208538Sraj 1440208538Sraj } 1441208538Sraj /* User wants to display properties */ 1442208538Sraj o = fdt_path_offset(fdtp, path); 1443208538Sraj 1444208538Sraj if (o < 0) { 1445208538Sraj sprintf(command_errbuf, "could not find node: '%s'", path); 1446208538Sraj rv = CMD_ERROR; 1447208538Sraj goto out; 1448208538Sraj } 1449208538Sraj 1450208538Sraj depth = 0; 1451208538Sraj while (depth >= 0) { 1452208538Sraj tag = fdt_next_tag(fdtp, o, &next); 1453208538Sraj switch (tag) { 1454208538Sraj case FDT_NOP: 1455208538Sraj break; 1456208538Sraj case FDT_PROP: 1457208538Sraj if (depth > 1) 1458208538Sraj /* Don't process properties of nested nodes */ 1459208538Sraj break; 1460208538Sraj 1461208538Sraj if (fdt_prop(o) != 0) { 1462208538Sraj sprintf(command_errbuf, "could not process " 1463208538Sraj "property"); 1464208538Sraj rv = CMD_ERROR; 1465208538Sraj goto out; 1466208538Sraj } 1467208538Sraj break; 1468208538Sraj case FDT_BEGIN_NODE: 1469208538Sraj depth++; 1470208538Sraj if (depth > FDT_MAX_DEPTH) { 1471208538Sraj printf("warning: nesting too deep: %d\n", 1472208538Sraj depth); 1473208538Sraj goto out; 1474208538Sraj } 1475208538Sraj break; 1476208538Sraj case FDT_END_NODE: 1477208538Sraj depth--; 1478208538Sraj if (depth == 0) 1479208538Sraj /* 1480208538Sraj * This is the end of our starting node, force 1481208538Sraj * the loop finish. 1482208538Sraj */ 1483208538Sraj depth--; 1484208538Sraj break; 1485208538Sraj } 1486208538Sraj o = next; 1487208538Sraj } 1488208538Srajout: 1489208538Sraj return (rv); 1490208538Sraj} 1491208538Sraj 1492208538Srajstatic int 1493208538Srajfdt_cmd_mkprop(int argc, char *argv[]) 1494208538Sraj{ 1495208538Sraj int o; 1496208538Sraj char *path, *propname, *value; 1497208538Sraj 1498208538Sraj path = (argc > 2) ? argv[2] : NULL; 1499208538Sraj 1500208538Sraj value = NULL; 1501208538Sraj 1502208538Sraj if (argc > 3) { 1503208538Sraj /* Merge property value strings into one */ 1504208538Sraj if (fdt_merge_strings(argc, argv, 3, &value) != 0) 1505208538Sraj return (CMD_ERROR); 1506208538Sraj } else 1507208538Sraj value = NULL; 1508208538Sraj 1509208538Sraj if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1510208538Sraj return (CMD_ERROR); 1511208538Sraj 1512208538Sraj if (fdt_modprop(o, propname, value, 1)) 1513208538Sraj return (CMD_ERROR); 1514208538Sraj 1515208538Sraj return (CMD_OK); 1516208538Sraj} 1517208538Sraj 1518208538Srajstatic int 1519208538Srajfdt_cmd_rm(int argc, char *argv[]) 1520208538Sraj{ 1521208538Sraj int o, rv; 1522208538Sraj char *path = NULL, *propname; 1523208538Sraj 1524208538Sraj if (argc > 2) 1525208538Sraj path = argv[2]; 1526208538Sraj else { 1527208538Sraj sprintf(command_errbuf, "no node/property name specified"); 1528208538Sraj return (CMD_ERROR); 1529208538Sraj } 1530208538Sraj 1531208538Sraj o = fdt_path_offset(fdtp, path); 1532208538Sraj if (o < 0) { 1533208538Sraj /* If node not found -- try to find & delete property */ 1534208538Sraj if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1535208538Sraj return (CMD_ERROR); 1536208538Sraj 1537208538Sraj if ((rv = fdt_delprop(fdtp, o, propname)) != 0) { 1538208538Sraj sprintf(command_errbuf, "could not delete" 1539208538Sraj "%s\n", (rv == -FDT_ERR_NOTFOUND) ? 1540208538Sraj "(property/node does not exist)" : ""); 1541208538Sraj return (CMD_ERROR); 1542208538Sraj 1543208538Sraj } else 1544208538Sraj return (CMD_OK); 1545208538Sraj } 1546208538Sraj /* If node exists -- remove node */ 1547208538Sraj rv = fdt_del_node(fdtp, o); 1548208538Sraj if (rv) { 1549208538Sraj sprintf(command_errbuf, "could not delete node"); 1550208538Sraj return (CMD_ERROR); 1551208538Sraj } 1552208538Sraj return (CMD_OK); 1553208538Sraj} 1554208538Sraj 1555208538Srajstatic int 1556208538Srajfdt_cmd_mknode(int argc, char *argv[]) 1557208538Sraj{ 1558208538Sraj int o, rv; 1559208538Sraj char *path = NULL, *nodename = NULL; 1560208538Sraj 1561208538Sraj if (argc > 2) 1562208538Sraj path = argv[2]; 1563208538Sraj else { 1564208538Sraj sprintf(command_errbuf, "no node name specified"); 1565208538Sraj return (CMD_ERROR); 1566208538Sraj } 1567208538Sraj 1568208538Sraj if (fdt_extract_nameloc(&path, &nodename, &o) != 0) 1569208538Sraj return (CMD_ERROR); 1570208538Sraj 1571208538Sraj rv = fdt_add_subnode(fdtp, o, nodename); 1572208538Sraj 1573208538Sraj if (rv < 0) { 1574233323Sraj if (rv == -FDT_ERR_NOSPACE) 1575233323Sraj sprintf(command_errbuf, 1576233323Sraj "Device tree blob is too small!\n"); 1577233323Sraj else 1578233323Sraj sprintf(command_errbuf, 1579233323Sraj "Could not add node!\n"); 1580208538Sraj return (CMD_ERROR); 1581208538Sraj } 1582208538Sraj return (CMD_OK); 1583208538Sraj} 1584208538Sraj 1585208538Srajstatic int 1586208538Srajfdt_cmd_pwd(int argc, char *argv[]) 1587208538Sraj{ 1588233323Sraj char line[FDT_CWD_LEN]; 1589208538Sraj 1590208538Sraj pager_open(); 1591208538Sraj sprintf(line, "%s\n", cwd); 1592208538Sraj pager_output(line); 1593208538Sraj pager_close(); 1594208538Sraj return (CMD_OK); 1595208538Sraj} 1596208538Sraj 1597208538Srajstatic int 1598243693Sgonzofdt_cmd_mres(int argc, char *argv[]) 1599243693Sgonzo{ 1600243693Sgonzo uint64_t start, size; 1601243693Sgonzo int i, total; 1602243693Sgonzo char line[80]; 1603243693Sgonzo 1604243693Sgonzo pager_open(); 1605243693Sgonzo total = fdt_num_mem_rsv(fdtp); 1606243693Sgonzo if (total > 0) { 1607243693Sgonzo pager_output("Reserved memory regions:\n"); 1608243693Sgonzo for (i = 0; i < total; i++) { 1609243693Sgonzo fdt_get_mem_rsv(fdtp, i, &start, &size); 1610243693Sgonzo sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n", 1611243693Sgonzo i, start, size); 1612243693Sgonzo pager_output(line); 1613243693Sgonzo } 1614243693Sgonzo } else 1615243693Sgonzo pager_output("No reserved memory regions\n"); 1616243693Sgonzo pager_close(); 1617243693Sgonzo 1618243693Sgonzo return (CMD_OK); 1619243693Sgonzo} 1620243693Sgonzo 1621243693Sgonzostatic int 1622208538Srajfdt_cmd_nyi(int argc, char *argv[]) 1623208538Sraj{ 1624208538Sraj 1625208538Sraj printf("command not yet implemented\n"); 1626208538Sraj return (CMD_ERROR); 1627208538Sraj} 1628