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