1 /*- 2 * Copyright (c) 2009-2010 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Semihalf under sponsorship from 6 * the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <stand.h> 34 #include <libfdt.h> 35 #include <fdt.h> 36 #include <sys/param.h> 37 #include <sys/linker.h> 38 #include <machine/elf.h> 39 40 #include "bootstrap.h" 41 #include "fdt_platform.h" 42 43 #ifdef DEBUG 44 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 45 printf(fmt,##args); } while (0) 46 #else 47 #define debugf(fmt, args...) 48 #endif 49 50 #define FDT_CWD_LEN 256 51 #define FDT_MAX_DEPTH 12 52 53 #define FDT_PROP_SEP " = " 54 55 #define COPYOUT(s,d,l) archsw.arch_copyout(s, d, l) 56 #define COPYIN(s,d,l) archsw.arch_copyin(s, d, l) 57 58 #define FDT_STATIC_DTB_SYMBOL "fdt_static_dtb" 59 60 #define CMD_REQUIRES_BLOB 0x01 61 62 /* Location of FDT yet to be loaded. */ 63 /* This may be in read-only memory, so can't be manipulated directly. */ 64 static struct fdt_header *fdt_to_load = NULL; 65 /* Location of FDT on heap. */ 66 /* This is the copy we actually manipulate. */ 67 static struct fdt_header *fdtp = NULL; 68 /* Size of FDT blob */ 69 static size_t fdtp_size = 0; 70 71 static int fdt_load_dtb(vm_offset_t va); 72 static void fdt_print_overlay_load_error(int err, const char *filename); 73 static int fdt_check_overlay_compatible(void *base_fdt, void *overlay_fdt); 74 75 static int fdt_cmd_nyi(int argc, char *argv[]); 76 static int fdt_load_dtb_overlays_string(const char * filenames); 77 78 static int fdt_cmd_addr(int argc, char *argv[]); 79 static int fdt_cmd_mkprop(int argc, char *argv[]); 80 static int fdt_cmd_cd(int argc, char *argv[]); 81 static int fdt_cmd_hdr(int argc, char *argv[]); 82 static int fdt_cmd_ls(int argc, char *argv[]); 83 static int fdt_cmd_prop(int argc, char *argv[]); 84 static int fdt_cmd_pwd(int argc, char *argv[]); 85 static int fdt_cmd_rm(int argc, char *argv[]); 86 static int fdt_cmd_mknode(int argc, char *argv[]); 87 static int fdt_cmd_mres(int argc, char *argv[]); 88 89 typedef int cmdf_t(int, char *[]); 90 91 struct cmdtab { 92 const char *name; 93 cmdf_t *handler; 94 int flags; 95 }; 96 97 static const struct cmdtab commands[] = { 98 { "addr", &fdt_cmd_addr, 0 }, 99 { "alias", &fdt_cmd_nyi, 0 }, 100 { "cd", &fdt_cmd_cd, CMD_REQUIRES_BLOB }, 101 { "header", &fdt_cmd_hdr, CMD_REQUIRES_BLOB }, 102 { "ls", &fdt_cmd_ls, CMD_REQUIRES_BLOB }, 103 { "mknode", &fdt_cmd_mknode, CMD_REQUIRES_BLOB }, 104 { "mkprop", &fdt_cmd_mkprop, CMD_REQUIRES_BLOB }, 105 { "mres", &fdt_cmd_mres, CMD_REQUIRES_BLOB }, 106 { "prop", &fdt_cmd_prop, CMD_REQUIRES_BLOB }, 107 { "pwd", &fdt_cmd_pwd, CMD_REQUIRES_BLOB }, 108 { "rm", &fdt_cmd_rm, CMD_REQUIRES_BLOB }, 109 { NULL, NULL } 110 }; 111 112 static char cwd[FDT_CWD_LEN] = "/"; 113 114 static vm_offset_t 115 fdt_find_static_dtb() 116 { 117 Elf_Ehdr *ehdr; 118 Elf_Shdr *shdr; 119 Elf_Sym sym; 120 vm_offset_t strtab, symtab, fdt_start; 121 uint64_t offs; 122 struct preloaded_file *kfp; 123 struct file_metadata *md; 124 char *strp; 125 int i, sym_count; 126 127 debugf("fdt_find_static_dtb()\n"); 128 129 sym_count = symtab = strtab = 0; 130 strp = NULL; 131 132 offs = __elfN(relocation_offset); 133 134 kfp = file_findfile(NULL, NULL); 135 if (kfp == NULL) 136 return (0); 137 138 /* Locate the dynamic symbols and strtab. */ 139 md = file_findmetadata(kfp, MODINFOMD_ELFHDR); 140 if (md == NULL) 141 return (0); 142 ehdr = (Elf_Ehdr *)md->md_data; 143 144 md = file_findmetadata(kfp, MODINFOMD_SHDR); 145 if (md == NULL) 146 return (0); 147 shdr = (Elf_Shdr *)md->md_data; 148 149 for (i = 0; i < ehdr->e_shnum; ++i) { 150 if (shdr[i].sh_type == SHT_DYNSYM && symtab == 0) { 151 symtab = shdr[i].sh_addr + offs; 152 sym_count = shdr[i].sh_size / sizeof(Elf_Sym); 153 } else if (shdr[i].sh_type == SHT_STRTAB && strtab == 0) { 154 strtab = shdr[i].sh_addr + offs; 155 } 156 } 157 158 /* 159 * The most efficient way to find a symbol would be to calculate a 160 * hash, find proper bucket and chain, and thus find a symbol. 161 * However, that would involve code duplication (e.g. for hash 162 * function). So we're using simpler and a bit slower way: we're 163 * iterating through symbols, searching for the one which name is 164 * 'equal' to 'fdt_static_dtb'. To speed up the process a little bit, 165 * we are eliminating symbols type of which is not STT_NOTYPE, or(and) 166 * those which binding attribute is not STB_GLOBAL. 167 */ 168 fdt_start = 0; 169 while (sym_count > 0 && fdt_start == 0) { 170 COPYOUT(symtab, &sym, sizeof(sym)); 171 symtab += sizeof(sym); 172 --sym_count; 173 if (ELF_ST_BIND(sym.st_info) != STB_GLOBAL || 174 ELF_ST_TYPE(sym.st_info) != STT_NOTYPE) 175 continue; 176 strp = strdupout(strtab + sym.st_name); 177 if (strcmp(strp, FDT_STATIC_DTB_SYMBOL) == 0) 178 fdt_start = (vm_offset_t)sym.st_value + offs; 179 free(strp); 180 } 181 return (fdt_start); 182 } 183 184 static int 185 fdt_load_dtb(vm_offset_t va) 186 { 187 struct fdt_header header; 188 int err; 189 190 debugf("fdt_load_dtb(0x%08jx)\n", (uintmax_t)va); 191 192 COPYOUT(va, &header, sizeof(header)); 193 err = fdt_check_header(&header); 194 if (err < 0) { 195 if (err == -FDT_ERR_BADVERSION) { 196 snprintf(command_errbuf, sizeof(command_errbuf), 197 "incompatible blob version: %d, should be: %d", 198 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION); 199 } else { 200 snprintf(command_errbuf, sizeof(command_errbuf), 201 "error validating blob: %s", fdt_strerror(err)); 202 } 203 return (1); 204 } 205 206 /* 207 * Release previous blob 208 */ 209 if (fdtp) 210 free(fdtp); 211 212 fdtp_size = fdt_totalsize(&header); 213 fdtp = malloc(fdtp_size); 214 215 if (fdtp == NULL) { 216 command_errmsg = "can't allocate memory for device tree copy"; 217 return (1); 218 } 219 220 COPYOUT(va, fdtp, fdtp_size); 221 debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size); 222 223 return (0); 224 } 225 226 int 227 fdt_load_dtb_addr(struct fdt_header *header) 228 { 229 int err; 230 231 debugf("fdt_load_dtb_addr(%p)\n", header); 232 233 fdtp_size = fdt_totalsize(header); 234 err = fdt_check_header(header); 235 if (err < 0) { 236 snprintf(command_errbuf, sizeof(command_errbuf), 237 "error validating blob: %s", fdt_strerror(err)); 238 return (err); 239 } 240 free(fdtp); 241 if ((fdtp = malloc(fdtp_size)) == NULL) { 242 command_errmsg = "can't allocate memory for device tree copy"; 243 return (1); 244 } 245 246 bcopy(header, fdtp, fdtp_size); 247 return (0); 248 } 249 250 int 251 fdt_load_dtb_file(const char * filename) 252 { 253 struct preloaded_file *bfp, *oldbfp; 254 int err; 255 256 debugf("fdt_load_dtb_file(%s)\n", filename); 257 258 oldbfp = file_findfile(NULL, "dtb"); 259 260 /* Attempt to load and validate a new dtb from a file. */ 261 if ((bfp = file_loadraw(filename, "dtb", 1)) == NULL) { 262 snprintf(command_errbuf, sizeof(command_errbuf), 263 "failed to load file '%s'", filename); 264 return (1); 265 } 266 if ((err = fdt_load_dtb(bfp->f_addr)) != 0) { 267 file_discard(bfp); 268 return (err); 269 } 270 271 /* A new dtb was validated, discard any previous file. */ 272 if (oldbfp) 273 file_discard(oldbfp); 274 return (0); 275 } 276 277 static int 278 fdt_load_dtb_overlay(const char * filename) 279 { 280 struct preloaded_file *bfp; 281 struct fdt_header header; 282 int err; 283 284 debugf("fdt_load_dtb_overlay(%s)\n", filename); 285 286 /* Attempt to load and validate a new dtb from a file. FDT_ERR_NOTFOUND 287 * is normally a libfdt error code, but libfdt would actually return 288 * -FDT_ERR_NOTFOUND. We re-purpose the error code here to convey a 289 * similar meaning: the file itself was not found, which can still be 290 * considered an error dealing with FDT pieces. 291 */ 292 if ((bfp = file_loadraw(filename, "dtbo", 1)) == NULL) 293 return (FDT_ERR_NOTFOUND); 294 295 COPYOUT(bfp->f_addr, &header, sizeof(header)); 296 err = fdt_check_header(&header); 297 298 if (err < 0) { 299 file_discard(bfp); 300 return (err); 301 } 302 303 return (0); 304 } 305 306 static void 307 fdt_print_overlay_load_error(int err, const char *filename) 308 { 309 310 switch (err) { 311 case FDT_ERR_NOTFOUND: 312 printf("%s: failed to load file\n", filename); 313 break; 314 case -FDT_ERR_BADVERSION: 315 printf("%s: incompatible blob version: %d, should be: %d\n", 316 filename, fdt_version(fdtp), 317 FDT_LAST_SUPPORTED_VERSION); 318 break; 319 default: 320 /* libfdt errs are negative */ 321 if (err < 0) 322 printf("%s: error validating blob: %s\n", 323 filename, fdt_strerror(err)); 324 else 325 printf("%s: unknown load error\n", filename); 326 break; 327 } 328 } 329 330 static int 331 fdt_load_dtb_overlays_string(const char * filenames) 332 { 333 char *names; 334 char *name, *name_ext; 335 char *comaptr; 336 int err, namesz; 337 338 debugf("fdt_load_dtb_overlays_string(%s)\n", filenames); 339 340 names = strdup(filenames); 341 if (names == NULL) 342 return (1); 343 name = names; 344 do { 345 comaptr = strchr(name, ','); 346 if (comaptr) 347 *comaptr = '\0'; 348 err = fdt_load_dtb_overlay(name); 349 if (err == FDT_ERR_NOTFOUND) { 350 /* Allocate enough to append ".dtbo" */ 351 namesz = strlen(name) + 6; 352 name_ext = malloc(namesz); 353 if (name_ext == NULL) { 354 fdt_print_overlay_load_error(err, name); 355 name = comaptr + 1; 356 continue; 357 } 358 snprintf(name_ext, namesz, "%s.dtbo", name); 359 err = fdt_load_dtb_overlay(name_ext); 360 free(name_ext); 361 } 362 /* Catch error with either initial load or fallback load */ 363 if (err != 0) 364 fdt_print_overlay_load_error(err, name); 365 name = comaptr + 1; 366 } while(comaptr); 367 368 free(names); 369 return (0); 370 } 371 372 /* 373 * fdt_check_overlay_compatible - check that the overlay_fdt is compatible with 374 * base_fdt before we attempt to apply it. It will need to re-calculate offsets 375 * in the base every time, rather than trying to cache them earlier in the 376 * process, because the overlay application process can/will invalidate a lot of 377 * offsets. 378 */ 379 static int 380 fdt_check_overlay_compatible(void *base_fdt, void *overlay_fdt) 381 { 382 const char *compat; 383 int compat_len, ocompat_len; 384 int oroot_offset, root_offset; 385 int slidx, sllen; 386 387 oroot_offset = fdt_path_offset(overlay_fdt, "/"); 388 if (oroot_offset < 0) 389 return (oroot_offset); 390 /* 391 * If /compatible in the overlay does not exist or if it is empty, then 392 * we're automatically compatible. We do this for the sake of rapid 393 * overlay development for overlays that aren't intended to be deployed. 394 * The user assumes the risk of using an overlay without /compatible. 395 */ 396 if (fdt_get_property(overlay_fdt, oroot_offset, "compatible", 397 &ocompat_len) == NULL || ocompat_len == 0) 398 return (0); 399 root_offset = fdt_path_offset(base_fdt, "/"); 400 if (root_offset < 0) 401 return (root_offset); 402 /* 403 * However, an empty or missing /compatible on the base is an error, 404 * because allowing this offers no advantages. 405 */ 406 if (fdt_get_property(base_fdt, root_offset, "compatible", 407 &compat_len) == NULL) 408 return (compat_len); 409 else if(compat_len == 0) 410 return (1); 411 412 slidx = 0; 413 compat = fdt_stringlist_get(overlay_fdt, oroot_offset, "compatible", 414 slidx, &sllen); 415 while (compat != NULL) { 416 if (fdt_stringlist_search(base_fdt, root_offset, "compatible", 417 compat) >= 0) 418 return (0); 419 ++slidx; 420 compat = fdt_stringlist_get(overlay_fdt, oroot_offset, 421 "compatible", slidx, &sllen); 422 }; 423 424 /* We've exhausted the overlay's /compatible property... no match */ 425 return (1); 426 } 427 428 void 429 fdt_apply_overlays() 430 { 431 struct preloaded_file *fp; 432 size_t max_overlay_size, next_fdtp_size; 433 size_t current_fdtp_size; 434 void *current_fdtp; 435 void *next_fdtp; 436 void *overlay; 437 int rv; 438 439 if ((fdtp == NULL) || (fdtp_size == 0)) 440 return; 441 442 max_overlay_size = 0; 443 for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) { 444 if (max_overlay_size < fp->f_size) 445 max_overlay_size = fp->f_size; 446 } 447 448 /* Nothing to apply */ 449 if (max_overlay_size == 0) 450 return; 451 452 overlay = malloc(max_overlay_size); 453 if (overlay == NULL) { 454 printf("failed to allocate memory for DTB blob with overlays\n"); 455 return; 456 } 457 current_fdtp = fdtp; 458 current_fdtp_size = fdtp_size; 459 for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) { 460 COPYOUT(fp->f_addr, overlay, fp->f_size); 461 /* Check compatible first to avoid unnecessary allocation */ 462 rv = fdt_check_overlay_compatible(current_fdtp, overlay); 463 if (rv != 0) { 464 printf("DTB overlay '%s' not compatible\n", fp->f_name); 465 continue; 466 } 467 printf("applying DTB overlay '%s'\n", fp->f_name); 468 next_fdtp_size = current_fdtp_size + fp->f_size; 469 next_fdtp = malloc(next_fdtp_size); 470 if (next_fdtp == NULL) { 471 /* 472 * Output warning, then move on to applying other 473 * overlays in case this one is simply too large. 474 */ 475 printf("failed to allocate memory for overlay base\n"); 476 continue; 477 } 478 rv = fdt_open_into(current_fdtp, next_fdtp, next_fdtp_size); 479 if (rv != 0) { 480 free(next_fdtp); 481 printf("failed to open base dtb into overlay base\n"); 482 continue; 483 } 484 /* Both overlay and next_fdtp may be modified in place */ 485 rv = fdt_overlay_apply(next_fdtp, overlay); 486 if (rv == 0) { 487 /* Rotate next -> current */ 488 if (current_fdtp != fdtp) 489 free(current_fdtp); 490 current_fdtp = next_fdtp; 491 current_fdtp_size = next_fdtp_size; 492 } else { 493 /* 494 * Assume here that the base we tried to apply on is 495 * either trashed or in an inconsistent state. Trying to 496 * load it might work, but it's better to discard it and 497 * play it safe. */ 498 free(next_fdtp); 499 printf("failed to apply overlay: %s\n", 500 fdt_strerror(rv)); 501 } 502 } 503 /* We could have failed to apply all overlays; then we do nothing */ 504 if (current_fdtp != fdtp) { 505 free(fdtp); 506 fdtp = current_fdtp; 507 fdtp_size = current_fdtp_size; 508 } 509 free(overlay); 510 } 511 512 int 513 fdt_setup_fdtp() 514 { 515 struct preloaded_file *bfp; 516 vm_offset_t va; 517 518 debugf("fdt_setup_fdtp()\n"); 519 520 /* If we already loaded a file, use it. */ 521 if ((bfp = file_findfile(NULL, "dtb")) != NULL) { 522 if (fdt_load_dtb(bfp->f_addr) == 0) { 523 printf("Using DTB from loaded file '%s'.\n", 524 bfp->f_name); 525 return (0); 526 } 527 } 528 529 /* If we were given the address of a valid blob in memory, use it. */ 530 if (fdt_to_load != NULL) { 531 if (fdt_load_dtb_addr(fdt_to_load) == 0) { 532 printf("Using DTB from memory address %p.\n", 533 fdt_to_load); 534 return (0); 535 } 536 } 537 538 if (fdt_platform_load_dtb() == 0) 539 return (0); 540 541 /* If there is a dtb compiled into the kernel, use it. */ 542 if ((va = fdt_find_static_dtb()) != 0) { 543 if (fdt_load_dtb(va) == 0) { 544 printf("Using DTB compiled into kernel.\n"); 545 return (0); 546 } 547 } 548 549 command_errmsg = "No device tree blob found!\n"; 550 return (1); 551 } 552 553 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \ 554 (cellbuf), (lim), (cellsize), 0); 555 556 /* Force using base 16 */ 557 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \ 558 (cellbuf), (lim), (cellsize), 16); 559 560 static int 561 _fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize, 562 uint8_t base) 563 { 564 const char *buf = str; 565 const char *end = str + strlen(str) - 2; 566 uint32_t *u32buf = NULL; 567 uint8_t *u8buf = NULL; 568 int cnt = 0; 569 570 if (cellsize == sizeof(uint32_t)) 571 u32buf = (uint32_t *)cellbuf; 572 else 573 u8buf = (uint8_t *)cellbuf; 574 575 if (lim == 0) 576 return (0); 577 578 while (buf < end) { 579 580 /* Skip white whitespace(s)/separators */ 581 while (!isxdigit(*buf) && buf < end) 582 buf++; 583 584 if (u32buf != NULL) 585 u32buf[cnt] = 586 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base)); 587 588 else 589 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base); 590 591 if (cnt + 1 <= lim - 1) 592 cnt++; 593 else 594 break; 595 buf++; 596 /* Find another number */ 597 while ((isxdigit(*buf) || *buf == 'x') && buf < end) 598 buf++; 599 } 600 return (cnt); 601 } 602 603 void 604 fdt_fixup_ethernet(const char *str, char *ethstr, int len) 605 { 606 uint8_t tmp_addr[6]; 607 608 /* Convert macaddr string into a vector of uints */ 609 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t)); 610 /* Set actual property to a value from vect */ 611 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr), 612 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t)); 613 } 614 615 void 616 fdt_fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq) 617 { 618 int lo, o = 0, o2, maxo = 0, depth; 619 const uint32_t zero = 0; 620 621 /* We want to modify every subnode of /cpus */ 622 o = fdt_path_offset(fdtp, "/cpus"); 623 if (o < 0) 624 return; 625 626 /* maxo should contain offset of node next to /cpus */ 627 depth = 0; 628 maxo = o; 629 while (depth != -1) 630 maxo = fdt_next_node(fdtp, maxo, &depth); 631 632 /* Find CPU frequency properties */ 633 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency", 634 &zero, sizeof(uint32_t)); 635 636 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero, 637 sizeof(uint32_t)); 638 639 lo = MIN(o, o2); 640 641 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) { 642 643 o = fdt_node_offset_by_prop_value(fdtp, lo, 644 "clock-frequency", &zero, sizeof(uint32_t)); 645 646 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency", 647 &zero, sizeof(uint32_t)); 648 649 /* We're only interested in /cpus subnode(s) */ 650 if (lo > maxo) 651 break; 652 653 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency", 654 (uint32_t)cpufreq); 655 656 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency", 657 (uint32_t)busfreq); 658 659 lo = MIN(o, o2); 660 } 661 } 662 663 #ifdef notyet 664 static int 665 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells) 666 { 667 int cells_in_tuple, i, tuples, tuple_size; 668 uint32_t cur_start, cur_size; 669 670 cells_in_tuple = (addr_cells + size_cells); 671 tuple_size = cells_in_tuple * sizeof(uint32_t); 672 tuples = len / tuple_size; 673 if (tuples == 0) 674 return (EINVAL); 675 676 for (i = 0; i < tuples; i++) { 677 if (addr_cells == 2) 678 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]); 679 else 680 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]); 681 682 if (size_cells == 2) 683 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]); 684 else 685 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]); 686 687 if (cur_size == 0) 688 return (EINVAL); 689 690 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n", 691 i, cur_start, cur_size); 692 } 693 return (0); 694 } 695 #endif 696 697 void 698 fdt_fixup_memory(struct fdt_mem_region *region, size_t num) 699 { 700 struct fdt_mem_region *curmr; 701 uint32_t addr_cells, size_cells; 702 uint32_t *addr_cellsp, *size_cellsp; 703 int err, i, len, memory, root; 704 size_t realmrno; 705 uint8_t *buf, *sb; 706 uint64_t rstart, rsize; 707 int reserved; 708 709 root = fdt_path_offset(fdtp, "/"); 710 if (root < 0) { 711 sprintf(command_errbuf, "Could not find root node !"); 712 return; 713 } 714 715 memory = fdt_path_offset(fdtp, "/memory"); 716 if (memory <= 0) { 717 /* Create proper '/memory' node. */ 718 memory = fdt_add_subnode(fdtp, root, "memory"); 719 if (memory <= 0) { 720 snprintf(command_errbuf, sizeof(command_errbuf), 721 "Could not fixup '/memory' " 722 "node, error code : %d!\n", memory); 723 return; 724 } 725 726 err = fdt_setprop(fdtp, memory, "device_type", "memory", 727 sizeof("memory")); 728 729 if (err < 0) 730 return; 731 } 732 733 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells", 734 NULL); 735 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL); 736 737 if (addr_cellsp == NULL || size_cellsp == NULL) { 738 snprintf(command_errbuf, sizeof(command_errbuf), 739 "Could not fixup '/memory' node : " 740 "%s %s property not found in root node!\n", 741 (!addr_cellsp) ? "#address-cells" : "", 742 (!size_cellsp) ? "#size-cells" : ""); 743 return; 744 } 745 746 addr_cells = fdt32_to_cpu(*addr_cellsp); 747 size_cells = fdt32_to_cpu(*size_cellsp); 748 749 /* 750 * Convert memreserve data to memreserve property 751 * Check if property already exists 752 */ 753 reserved = fdt_num_mem_rsv(fdtp); 754 if (reserved && 755 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) { 756 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t); 757 sb = buf = (uint8_t *)malloc(len); 758 if (!buf) 759 return; 760 761 bzero(buf, len); 762 763 for (i = 0; i < reserved; i++) { 764 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize)) 765 break; 766 if (rsize) { 767 /* Ensure endianness, and put cells into a buffer */ 768 if (addr_cells == 2) 769 *(uint64_t *)buf = 770 cpu_to_fdt64(rstart); 771 else 772 *(uint32_t *)buf = 773 cpu_to_fdt32(rstart); 774 775 buf += sizeof(uint32_t) * addr_cells; 776 if (size_cells == 2) 777 *(uint64_t *)buf = 778 cpu_to_fdt64(rsize); 779 else 780 *(uint32_t *)buf = 781 cpu_to_fdt32(rsize); 782 783 buf += sizeof(uint32_t) * size_cells; 784 } 785 } 786 787 /* Set property */ 788 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0) 789 printf("Could not fixup 'memreserve' property.\n"); 790 791 free(sb); 792 } 793 794 /* Count valid memory regions entries in sysinfo. */ 795 realmrno = num; 796 for (i = 0; i < num; i++) 797 if (region[i].start == 0 && region[i].size == 0) 798 realmrno--; 799 800 if (realmrno == 0) { 801 sprintf(command_errbuf, "Could not fixup '/memory' node : " 802 "sysinfo doesn't contain valid memory regions info!\n"); 803 return; 804 } 805 806 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t); 807 sb = buf = (uint8_t *)malloc(len); 808 if (!buf) 809 return; 810 811 bzero(buf, len); 812 813 for (i = 0; i < num; i++) { 814 curmr = ®ion[i]; 815 if (curmr->size != 0) { 816 /* Ensure endianness, and put cells into a buffer */ 817 if (addr_cells == 2) 818 *(uint64_t *)buf = 819 cpu_to_fdt64(curmr->start); 820 else 821 *(uint32_t *)buf = 822 cpu_to_fdt32(curmr->start); 823 824 buf += sizeof(uint32_t) * addr_cells; 825 if (size_cells == 2) 826 *(uint64_t *)buf = 827 cpu_to_fdt64(curmr->size); 828 else 829 *(uint32_t *)buf = 830 cpu_to_fdt32(curmr->size); 831 832 buf += sizeof(uint32_t) * size_cells; 833 } 834 } 835 836 /* Set property */ 837 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0) 838 sprintf(command_errbuf, "Could not fixup '/memory' node.\n"); 839 840 free(sb); 841 } 842 843 void 844 fdt_fixup_stdout(const char *str) 845 { 846 char *ptr; 847 int serialno; 848 int len, no, sero; 849 const struct fdt_property *prop; 850 char *tmp[10]; 851 852 ptr = (char *)str + strlen(str) - 1; 853 while (ptr > str && isdigit(*(str - 1))) 854 str--; 855 856 if (ptr == str) 857 return; 858 859 serialno = (int)strtol(ptr, NULL, 0); 860 no = fdt_path_offset(fdtp, "/chosen"); 861 if (no < 0) 862 return; 863 864 prop = fdt_get_property(fdtp, no, "stdout", &len); 865 866 /* If /chosen/stdout does not extist, create it */ 867 if (prop == NULL || (prop != NULL && len == 0)) { 868 869 bzero(tmp, 10 * sizeof(char)); 870 strcpy((char *)&tmp, "serial"); 871 if (strlen(ptr) > 3) 872 /* Serial number too long */ 873 return; 874 875 strncpy((char *)tmp + 6, ptr, 3); 876 sero = fdt_path_offset(fdtp, (const char *)tmp); 877 if (sero < 0) 878 /* 879 * If serial device we're trying to assign 880 * stdout to doesn't exist in DT -- return. 881 */ 882 return; 883 884 fdt_setprop(fdtp, no, "stdout", &tmp, 885 strlen((char *)&tmp) + 1); 886 fdt_setprop(fdtp, no, "stdin", &tmp, 887 strlen((char *)&tmp) + 1); 888 } 889 } 890 891 void 892 fdt_load_dtb_overlays(const char *extras) 893 { 894 const char *s; 895 896 /* Any extra overlays supplied by pre-loader environment */ 897 if (extras != NULL && *extras != '\0') { 898 printf("Loading DTB overlays: '%s'\n", extras); 899 fdt_load_dtb_overlays_string(extras); 900 } 901 902 /* Any overlays supplied by loader environment */ 903 s = getenv("fdt_overlays"); 904 if (s != NULL && *s != '\0') { 905 printf("Loading DTB overlays: '%s'\n", s); 906 fdt_load_dtb_overlays_string(s); 907 } 908 } 909 910 /* 911 * Locate the blob, fix it up and return its location. 912 */ 913 static int 914 fdt_fixup(void) 915 { 916 int chosen, len; 917 918 len = 0; 919 920 debugf("fdt_fixup()\n"); 921 922 if (fdtp == NULL && fdt_setup_fdtp() != 0) 923 return (0); 924 925 /* Create /chosen node (if not exists) */ 926 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) == 927 -FDT_ERR_NOTFOUND) 928 chosen = fdt_add_subnode(fdtp, 0, "chosen"); 929 930 /* Value assigned to fixup-applied does not matter. */ 931 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL)) 932 return (1); 933 934 fdt_platform_fixups(); 935 936 /* 937 * Re-fetch the /chosen subnode; our fixups may apply overlays or add 938 * nodes/properties that invalidate the offset we grabbed or created 939 * above, so we can no longer trust it. 940 */ 941 chosen = fdt_subnode_offset(fdtp, 0, "chosen"); 942 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0); 943 return (1); 944 } 945 946 /* 947 * Copy DTB blob to specified location and return size 948 */ 949 int 950 fdt_copy(vm_offset_t va) 951 { 952 int err; 953 debugf("fdt_copy va 0x%08x\n", va); 954 if (fdtp == NULL) { 955 err = fdt_setup_fdtp(); 956 if (err) { 957 printf("No valid device tree blob found!\n"); 958 return (0); 959 } 960 } 961 962 if (fdt_fixup() == 0) 963 return (0); 964 965 COPYIN(fdtp, va, fdtp_size); 966 return (fdtp_size); 967 } 968 969 970 971 int 972 command_fdt_internal(int argc, char *argv[]) 973 { 974 cmdf_t *cmdh; 975 int flags; 976 char *cmd; 977 int i, err; 978 979 if (argc < 2) { 980 command_errmsg = "usage is 'fdt <command> [<args>]"; 981 return (CMD_ERROR); 982 } 983 984 /* 985 * Validate fdt <command>. 986 */ 987 cmd = strdup(argv[1]); 988 i = 0; 989 cmdh = NULL; 990 while (!(commands[i].name == NULL)) { 991 if (strcmp(cmd, commands[i].name) == 0) { 992 /* found it */ 993 cmdh = commands[i].handler; 994 flags = commands[i].flags; 995 break; 996 } 997 i++; 998 } 999 if (cmdh == NULL) { 1000 command_errmsg = "unknown command"; 1001 return (CMD_ERROR); 1002 } 1003 1004 if (flags & CMD_REQUIRES_BLOB) { 1005 /* 1006 * Check if uboot env vars were parsed already. If not, do it now. 1007 */ 1008 if (fdt_fixup() == 0) 1009 return (CMD_ERROR); 1010 } 1011 1012 /* 1013 * Call command handler. 1014 */ 1015 err = (*cmdh)(argc, argv); 1016 1017 return (err); 1018 } 1019 1020 static int 1021 fdt_cmd_addr(int argc, char *argv[]) 1022 { 1023 struct preloaded_file *fp; 1024 struct fdt_header *hdr; 1025 const char *addr; 1026 char *cp; 1027 1028 fdt_to_load = NULL; 1029 1030 if (argc > 2) 1031 addr = argv[2]; 1032 else { 1033 sprintf(command_errbuf, "no address specified"); 1034 return (CMD_ERROR); 1035 } 1036 1037 hdr = (struct fdt_header *)strtoul(addr, &cp, 16); 1038 if (cp == addr) { 1039 snprintf(command_errbuf, sizeof(command_errbuf), 1040 "Invalid address: %s", addr); 1041 return (CMD_ERROR); 1042 } 1043 1044 while ((fp = file_findfile(NULL, "dtb")) != NULL) { 1045 file_discard(fp); 1046 } 1047 1048 fdt_to_load = hdr; 1049 return (CMD_OK); 1050 } 1051 1052 static int 1053 fdt_cmd_cd(int argc, char *argv[]) 1054 { 1055 char *path; 1056 char tmp[FDT_CWD_LEN]; 1057 int len, o; 1058 1059 path = (argc > 2) ? argv[2] : "/"; 1060 1061 if (path[0] == '/') { 1062 len = strlen(path); 1063 if (len >= FDT_CWD_LEN) 1064 goto fail; 1065 } else { 1066 /* Handle path specification relative to cwd */ 1067 len = strlen(cwd) + strlen(path) + 1; 1068 if (len >= FDT_CWD_LEN) 1069 goto fail; 1070 1071 strcpy(tmp, cwd); 1072 strcat(tmp, "/"); 1073 strcat(tmp, path); 1074 path = tmp; 1075 } 1076 1077 o = fdt_path_offset(fdtp, path); 1078 if (o < 0) { 1079 snprintf(command_errbuf, sizeof(command_errbuf), 1080 "could not find node: '%s'", path); 1081 return (CMD_ERROR); 1082 } 1083 1084 strcpy(cwd, path); 1085 return (CMD_OK); 1086 1087 fail: 1088 snprintf(command_errbuf, sizeof(command_errbuf), 1089 "path too long: %d, max allowed: %d", len, FDT_CWD_LEN - 1); 1090 return (CMD_ERROR); 1091 } 1092 1093 static int 1094 fdt_cmd_hdr(int argc __unused, char *argv[] __unused) 1095 { 1096 char line[80]; 1097 int ver; 1098 1099 if (fdtp == NULL) { 1100 command_errmsg = "no device tree blob pointer?!"; 1101 return (CMD_ERROR); 1102 } 1103 1104 ver = fdt_version(fdtp); 1105 pager_open(); 1106 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp); 1107 if (pager_output(line)) 1108 goto out; 1109 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp)); 1110 if (pager_output(line)) 1111 goto out; 1112 sprintf(line, " size = %d\n", fdt_totalsize(fdtp)); 1113 if (pager_output(line)) 1114 goto out; 1115 sprintf(line, " off_dt_struct = 0x%08x\n", 1116 fdt_off_dt_struct(fdtp)); 1117 if (pager_output(line)) 1118 goto out; 1119 sprintf(line, " off_dt_strings = 0x%08x\n", 1120 fdt_off_dt_strings(fdtp)); 1121 if (pager_output(line)) 1122 goto out; 1123 sprintf(line, " off_mem_rsvmap = 0x%08x\n", 1124 fdt_off_mem_rsvmap(fdtp)); 1125 if (pager_output(line)) 1126 goto out; 1127 sprintf(line, " version = %d\n", ver); 1128 if (pager_output(line)) 1129 goto out; 1130 sprintf(line, " last compatible version = %d\n", 1131 fdt_last_comp_version(fdtp)); 1132 if (pager_output(line)) 1133 goto out; 1134 if (ver >= 2) { 1135 sprintf(line, " boot_cpuid = %d\n", 1136 fdt_boot_cpuid_phys(fdtp)); 1137 if (pager_output(line)) 1138 goto out; 1139 } 1140 if (ver >= 3) { 1141 sprintf(line, " size_dt_strings = %d\n", 1142 fdt_size_dt_strings(fdtp)); 1143 if (pager_output(line)) 1144 goto out; 1145 } 1146 if (ver >= 17) { 1147 sprintf(line, " size_dt_struct = %d\n", 1148 fdt_size_dt_struct(fdtp)); 1149 if (pager_output(line)) 1150 goto out; 1151 } 1152 out: 1153 pager_close(); 1154 1155 return (CMD_OK); 1156 } 1157 1158 static int 1159 fdt_cmd_ls(int argc, char *argv[]) 1160 { 1161 const char *prevname[FDT_MAX_DEPTH] = { NULL }; 1162 const char *name; 1163 char *path; 1164 int i, o, depth; 1165 1166 path = (argc > 2) ? argv[2] : NULL; 1167 if (path == NULL) 1168 path = cwd; 1169 1170 o = fdt_path_offset(fdtp, path); 1171 if (o < 0) { 1172 snprintf(command_errbuf, sizeof(command_errbuf), 1173 "could not find node: '%s'", path); 1174 return (CMD_ERROR); 1175 } 1176 1177 for (depth = 0; 1178 (o >= 0) && (depth >= 0); 1179 o = fdt_next_node(fdtp, o, &depth)) { 1180 1181 name = fdt_get_name(fdtp, o, NULL); 1182 1183 if (depth > FDT_MAX_DEPTH) { 1184 printf("max depth exceeded: %d\n", depth); 1185 continue; 1186 } 1187 1188 prevname[depth] = name; 1189 1190 /* Skip root (i = 1) when printing devices */ 1191 for (i = 1; i <= depth; i++) { 1192 if (prevname[i] == NULL) 1193 break; 1194 1195 if (strcmp(cwd, "/") == 0) 1196 printf("/"); 1197 printf("%s", prevname[i]); 1198 } 1199 printf("\n"); 1200 } 1201 1202 return (CMD_OK); 1203 } 1204 1205 static __inline int 1206 isprint(int c) 1207 { 1208 1209 return (c >= ' ' && c <= 0x7e); 1210 } 1211 1212 static int 1213 fdt_isprint(const void *data, int len, int *count) 1214 { 1215 const char *d; 1216 char ch; 1217 int yesno, i; 1218 1219 if (len == 0) 1220 return (0); 1221 1222 d = (const char *)data; 1223 if (d[len - 1] != '\0') 1224 return (0); 1225 1226 *count = 0; 1227 yesno = 1; 1228 for (i = 0; i < len; i++) { 1229 ch = *(d + i); 1230 if (isprint(ch) || (ch == '\0' && i > 0)) { 1231 /* Count strings */ 1232 if (ch == '\0') 1233 (*count)++; 1234 continue; 1235 } 1236 1237 yesno = 0; 1238 break; 1239 } 1240 1241 return (yesno); 1242 } 1243 1244 static int 1245 fdt_data_str(const void *data, int len, int count, char **buf) 1246 { 1247 char *b, *tmp; 1248 const char *d; 1249 int buf_len, i, l; 1250 1251 /* 1252 * Calculate the length for the string and allocate memory. 1253 * 1254 * Note that 'len' already includes at least one terminator. 1255 */ 1256 buf_len = len; 1257 if (count > 1) { 1258 /* 1259 * Each token had already a terminator buried in 'len', but we 1260 * only need one eventually, don't count space for these. 1261 */ 1262 buf_len -= count - 1; 1263 1264 /* Each consecutive token requires a ", " separator. */ 1265 buf_len += count * 2; 1266 } 1267 1268 /* Add some space for surrounding double quotes. */ 1269 buf_len += count * 2; 1270 1271 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */ 1272 b = (char *)malloc(buf_len); 1273 tmp = (char *)malloc(buf_len); 1274 if (b == NULL) 1275 goto error; 1276 1277 if (tmp == NULL) { 1278 free(b); 1279 goto error; 1280 } 1281 1282 b[0] = '\0'; 1283 1284 /* 1285 * Now that we have space, format the string. 1286 */ 1287 i = 0; 1288 do { 1289 d = (const char *)data + i; 1290 l = strlen(d) + 1; 1291 1292 sprintf(tmp, "\"%s\"%s", d, 1293 (i + l) < len ? ", " : ""); 1294 strcat(b, tmp); 1295 1296 i += l; 1297 1298 } while (i < len); 1299 *buf = b; 1300 1301 free(tmp); 1302 1303 return (0); 1304 error: 1305 return (1); 1306 } 1307 1308 static int 1309 fdt_data_cell(const void *data, int len, char **buf) 1310 { 1311 char *b, *tmp; 1312 const uint32_t *c; 1313 int count, i, l; 1314 1315 /* Number of cells */ 1316 count = len / 4; 1317 1318 /* 1319 * Calculate the length for the string and allocate memory. 1320 */ 1321 1322 /* Each byte translates to 2 output characters */ 1323 l = len * 2; 1324 if (count > 1) { 1325 /* Each consecutive cell requires a " " separator. */ 1326 l += (count - 1) * 1; 1327 } 1328 /* Each cell will have a "0x" prefix */ 1329 l += count * 2; 1330 /* Space for surrounding <> and terminator */ 1331 l += 3; 1332 1333 b = (char *)malloc(l); 1334 tmp = (char *)malloc(l); 1335 if (b == NULL) 1336 goto error; 1337 1338 if (tmp == NULL) { 1339 free(b); 1340 goto error; 1341 } 1342 1343 b[0] = '\0'; 1344 strcat(b, "<"); 1345 1346 for (i = 0; i < len; i += 4) { 1347 c = (const uint32_t *)((const uint8_t *)data + i); 1348 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c), 1349 i < (len - 4) ? " " : ""); 1350 strcat(b, tmp); 1351 } 1352 strcat(b, ">"); 1353 *buf = b; 1354 1355 free(tmp); 1356 1357 return (0); 1358 error: 1359 return (1); 1360 } 1361 1362 static int 1363 fdt_data_bytes(const void *data, int len, char **buf) 1364 { 1365 char *b, *tmp; 1366 const char *d; 1367 int i, l; 1368 1369 /* 1370 * Calculate the length for the string and allocate memory. 1371 */ 1372 1373 /* Each byte translates to 2 output characters */ 1374 l = len * 2; 1375 if (len > 1) 1376 /* Each consecutive byte requires a " " separator. */ 1377 l += (len - 1) * 1; 1378 /* Each byte will have a "0x" prefix */ 1379 l += len * 2; 1380 /* Space for surrounding [] and terminator. */ 1381 l += 3; 1382 1383 b = (char *)malloc(l); 1384 tmp = (char *)malloc(l); 1385 if (b == NULL) 1386 goto error; 1387 1388 if (tmp == NULL) { 1389 free(b); 1390 goto error; 1391 } 1392 1393 b[0] = '\0'; 1394 strcat(b, "["); 1395 1396 for (i = 0, d = data; i < len; i++) { 1397 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : ""); 1398 strcat(b, tmp); 1399 } 1400 strcat(b, "]"); 1401 *buf = b; 1402 1403 free(tmp); 1404 1405 return (0); 1406 error: 1407 return (1); 1408 } 1409 1410 static int 1411 fdt_data_fmt(const void *data, int len, char **buf) 1412 { 1413 int count; 1414 1415 if (len == 0) { 1416 *buf = NULL; 1417 return (1); 1418 } 1419 1420 if (fdt_isprint(data, len, &count)) 1421 return (fdt_data_str(data, len, count, buf)); 1422 1423 else if ((len % 4) == 0) 1424 return (fdt_data_cell(data, len, buf)); 1425 1426 else 1427 return (fdt_data_bytes(data, len, buf)); 1428 } 1429 1430 static int 1431 fdt_prop(int offset) 1432 { 1433 char *line, *buf; 1434 const struct fdt_property *prop; 1435 const char *name; 1436 const void *data; 1437 int len, rv; 1438 1439 line = NULL; 1440 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop)); 1441 if (prop == NULL) 1442 return (1); 1443 1444 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff)); 1445 len = fdt32_to_cpu(prop->len); 1446 1447 rv = 0; 1448 buf = NULL; 1449 if (len == 0) { 1450 /* Property without value */ 1451 line = (char *)malloc(strlen(name) + 2); 1452 if (line == NULL) { 1453 rv = 2; 1454 goto out2; 1455 } 1456 sprintf(line, "%s\n", name); 1457 goto out1; 1458 } 1459 1460 /* 1461 * Process property with value 1462 */ 1463 data = prop->data; 1464 1465 if (fdt_data_fmt(data, len, &buf) != 0) { 1466 rv = 3; 1467 goto out2; 1468 } 1469 1470 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) + 1471 strlen(buf) + 2); 1472 if (line == NULL) { 1473 sprintf(command_errbuf, "could not allocate space for string"); 1474 rv = 4; 1475 goto out2; 1476 } 1477 1478 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf); 1479 1480 out1: 1481 pager_open(); 1482 pager_output(line); 1483 pager_close(); 1484 1485 out2: 1486 if (buf) 1487 free(buf); 1488 1489 if (line) 1490 free(line); 1491 1492 return (rv); 1493 } 1494 1495 static int 1496 fdt_modprop(int nodeoff, char *propname, void *value, char mode) 1497 { 1498 uint32_t cells[100]; 1499 const char *buf; 1500 int len, rv; 1501 const struct fdt_property *p; 1502 1503 p = fdt_get_property(fdtp, nodeoff, propname, NULL); 1504 1505 if (p != NULL) { 1506 if (mode == 1) { 1507 /* Adding inexistant value in mode 1 is forbidden */ 1508 sprintf(command_errbuf, "property already exists!"); 1509 return (CMD_ERROR); 1510 } 1511 } else if (mode == 0) { 1512 sprintf(command_errbuf, "property does not exist!"); 1513 return (CMD_ERROR); 1514 } 1515 len = strlen(value); 1516 rv = 0; 1517 buf = value; 1518 1519 switch (*buf) { 1520 case '&': 1521 /* phandles */ 1522 break; 1523 case '<': 1524 /* Data cells */ 1525 len = fdt_strtovect(buf, (void *)&cells, 100, 1526 sizeof(uint32_t)); 1527 1528 rv = fdt_setprop(fdtp, nodeoff, propname, &cells, 1529 len * sizeof(uint32_t)); 1530 break; 1531 case '[': 1532 /* Data bytes */ 1533 len = fdt_strtovect(buf, (void *)&cells, 100, 1534 sizeof(uint8_t)); 1535 1536 rv = fdt_setprop(fdtp, nodeoff, propname, &cells, 1537 len * sizeof(uint8_t)); 1538 break; 1539 case '"': 1540 default: 1541 /* Default -- string */ 1542 rv = fdt_setprop_string(fdtp, nodeoff, propname, value); 1543 break; 1544 } 1545 1546 if (rv != 0) { 1547 if (rv == -FDT_ERR_NOSPACE) 1548 sprintf(command_errbuf, 1549 "Device tree blob is too small!\n"); 1550 else 1551 sprintf(command_errbuf, 1552 "Could not add/modify property!\n"); 1553 } 1554 return (rv); 1555 } 1556 1557 /* Merge strings from argv into a single string */ 1558 static int 1559 fdt_merge_strings(int argc, char *argv[], int start, char **buffer) 1560 { 1561 char *buf; 1562 int i, idx, sz; 1563 1564 *buffer = NULL; 1565 sz = 0; 1566 1567 for (i = start; i < argc; i++) 1568 sz += strlen(argv[i]); 1569 1570 /* Additional bytes for whitespaces between args */ 1571 sz += argc - start; 1572 1573 buf = (char *)malloc(sizeof(char) * sz); 1574 if (buf == NULL) { 1575 sprintf(command_errbuf, "could not allocate space " 1576 "for string"); 1577 return (1); 1578 } 1579 bzero(buf, sizeof(char) * sz); 1580 1581 idx = 0; 1582 for (i = start, idx = 0; i < argc; i++) { 1583 strcpy(buf + idx, argv[i]); 1584 idx += strlen(argv[i]); 1585 buf[idx] = ' '; 1586 idx++; 1587 } 1588 buf[sz - 1] = '\0'; 1589 *buffer = buf; 1590 return (0); 1591 } 1592 1593 /* Extract offset and name of node/property from a given path */ 1594 static int 1595 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff) 1596 { 1597 int o; 1598 char *path = *pathp, *name = NULL, *subpath = NULL; 1599 1600 subpath = strrchr(path, '/'); 1601 if (subpath == NULL) { 1602 o = fdt_path_offset(fdtp, cwd); 1603 name = path; 1604 path = (char *)&cwd; 1605 } else { 1606 *subpath = '\0'; 1607 if (strlen(path) == 0) 1608 path = cwd; 1609 1610 name = subpath + 1; 1611 o = fdt_path_offset(fdtp, path); 1612 } 1613 1614 if (strlen(name) == 0) { 1615 sprintf(command_errbuf, "name not specified"); 1616 return (1); 1617 } 1618 if (o < 0) { 1619 snprintf(command_errbuf, sizeof(command_errbuf), 1620 "could not find node: '%s'", path); 1621 return (1); 1622 } 1623 *namep = name; 1624 *nodeoff = o; 1625 *pathp = path; 1626 return (0); 1627 } 1628 1629 static int 1630 fdt_cmd_prop(int argc, char *argv[]) 1631 { 1632 char *path, *propname, *value; 1633 int o, next, depth, rv; 1634 uint32_t tag; 1635 1636 path = (argc > 2) ? argv[2] : NULL; 1637 1638 value = NULL; 1639 1640 if (argc > 3) { 1641 /* Merge property value strings into one */ 1642 if (fdt_merge_strings(argc, argv, 3, &value) != 0) 1643 return (CMD_ERROR); 1644 } else 1645 value = NULL; 1646 1647 if (path == NULL) 1648 path = cwd; 1649 1650 rv = CMD_OK; 1651 1652 if (value) { 1653 /* If value is specified -- try to modify prop. */ 1654 if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1655 return (CMD_ERROR); 1656 1657 rv = fdt_modprop(o, propname, value, 0); 1658 if (rv) 1659 return (CMD_ERROR); 1660 return (CMD_OK); 1661 1662 } 1663 /* User wants to display properties */ 1664 o = fdt_path_offset(fdtp, path); 1665 1666 if (o < 0) { 1667 snprintf(command_errbuf, sizeof(command_errbuf), 1668 "could not find node: '%s'", path); 1669 rv = CMD_ERROR; 1670 goto out; 1671 } 1672 1673 depth = 0; 1674 while (depth >= 0) { 1675 tag = fdt_next_tag(fdtp, o, &next); 1676 switch (tag) { 1677 case FDT_NOP: 1678 break; 1679 case FDT_PROP: 1680 if (depth > 1) 1681 /* Don't process properties of nested nodes */ 1682 break; 1683 1684 if (fdt_prop(o) != 0) { 1685 sprintf(command_errbuf, "could not process " 1686 "property"); 1687 rv = CMD_ERROR; 1688 goto out; 1689 } 1690 break; 1691 case FDT_BEGIN_NODE: 1692 depth++; 1693 if (depth > FDT_MAX_DEPTH) { 1694 printf("warning: nesting too deep: %d\n", 1695 depth); 1696 goto out; 1697 } 1698 break; 1699 case FDT_END_NODE: 1700 depth--; 1701 if (depth == 0) 1702 /* 1703 * This is the end of our starting node, force 1704 * the loop finish. 1705 */ 1706 depth--; 1707 break; 1708 } 1709 o = next; 1710 } 1711 out: 1712 return (rv); 1713 } 1714 1715 static int 1716 fdt_cmd_mkprop(int argc, char *argv[]) 1717 { 1718 int o; 1719 char *path, *propname, *value; 1720 1721 path = (argc > 2) ? argv[2] : NULL; 1722 1723 value = NULL; 1724 1725 if (argc > 3) { 1726 /* Merge property value strings into one */ 1727 if (fdt_merge_strings(argc, argv, 3, &value) != 0) 1728 return (CMD_ERROR); 1729 } else 1730 value = NULL; 1731 1732 if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1733 return (CMD_ERROR); 1734 1735 if (fdt_modprop(o, propname, value, 1)) 1736 return (CMD_ERROR); 1737 1738 return (CMD_OK); 1739 } 1740 1741 static int 1742 fdt_cmd_rm(int argc, char *argv[]) 1743 { 1744 int o, rv; 1745 char *path = NULL, *propname; 1746 1747 if (argc > 2) 1748 path = argv[2]; 1749 else { 1750 sprintf(command_errbuf, "no node/property name specified"); 1751 return (CMD_ERROR); 1752 } 1753 1754 o = fdt_path_offset(fdtp, path); 1755 if (o < 0) { 1756 /* If node not found -- try to find & delete property */ 1757 if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1758 return (CMD_ERROR); 1759 1760 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) { 1761 snprintf(command_errbuf, sizeof(command_errbuf), 1762 "could not delete %s\n", 1763 (rv == -FDT_ERR_NOTFOUND) ? 1764 "(property/node does not exist)" : ""); 1765 return (CMD_ERROR); 1766 1767 } else 1768 return (CMD_OK); 1769 } 1770 /* If node exists -- remove node */ 1771 rv = fdt_del_node(fdtp, o); 1772 if (rv) { 1773 sprintf(command_errbuf, "could not delete node"); 1774 return (CMD_ERROR); 1775 } 1776 return (CMD_OK); 1777 } 1778 1779 static int 1780 fdt_cmd_mknode(int argc, char *argv[]) 1781 { 1782 int o, rv; 1783 char *path = NULL, *nodename = NULL; 1784 1785 if (argc > 2) 1786 path = argv[2]; 1787 else { 1788 sprintf(command_errbuf, "no node name specified"); 1789 return (CMD_ERROR); 1790 } 1791 1792 if (fdt_extract_nameloc(&path, &nodename, &o) != 0) 1793 return (CMD_ERROR); 1794 1795 rv = fdt_add_subnode(fdtp, o, nodename); 1796 1797 if (rv < 0) { 1798 if (rv == -FDT_ERR_NOSPACE) 1799 sprintf(command_errbuf, 1800 "Device tree blob is too small!\n"); 1801 else 1802 sprintf(command_errbuf, 1803 "Could not add node!\n"); 1804 return (CMD_ERROR); 1805 } 1806 return (CMD_OK); 1807 } 1808 1809 static int 1810 fdt_cmd_pwd(int argc, char *argv[]) 1811 { 1812 char line[FDT_CWD_LEN]; 1813 1814 pager_open(); 1815 sprintf(line, "%s\n", cwd); 1816 pager_output(line); 1817 pager_close(); 1818 return (CMD_OK); 1819 } 1820 1821 static int 1822 fdt_cmd_mres(int argc, char *argv[]) 1823 { 1824 uint64_t start, size; 1825 int i, total; 1826 char line[80]; 1827 1828 pager_open(); 1829 total = fdt_num_mem_rsv(fdtp); 1830 if (total > 0) { 1831 if (pager_output("Reserved memory regions:\n")) 1832 goto out; 1833 for (i = 0; i < total; i++) { 1834 fdt_get_mem_rsv(fdtp, i, &start, &size); 1835 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n", 1836 i, start, size); 1837 if (pager_output(line)) 1838 goto out; 1839 } 1840 } else 1841 pager_output("No reserved memory regions\n"); 1842 out: 1843 pager_close(); 1844 1845 return (CMD_OK); 1846 } 1847 1848 static int 1849 fdt_cmd_nyi(int argc, char *argv[]) 1850 { 1851 1852 printf("command not yet implemented\n"); 1853 return (CMD_ERROR); 1854 } 1855