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 *new_fdtp; 442 void *next_fdtp; 443 void *overlay; 444 int rv; 445 446 if ((fdtp == NULL) || (fdtp_size == 0)) 447 return; 448 449 new_fdtp = NULL; 450 max_overlay_size = 0; 451 for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) { 452 if (max_overlay_size < fp->f_size) 453 max_overlay_size = fp->f_size; 454 } 455 456 /* Nothing to apply */ 457 if (max_overlay_size == 0) 458 return; 459 460 overlay = malloc(max_overlay_size); 461 if (overlay == NULL) { 462 printf("failed to allocate memory for DTB blob with overlays\n"); 463 return; 464 } 465 current_fdtp = fdtp; 466 current_fdtp_size = fdtp_size; 467 for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) { 468 COPYOUT(fp->f_addr, overlay, fp->f_size); 469 /* Check compatible first to avoid unnecessary allocation */ 470 rv = fdt_check_overlay_compatible(current_fdtp, overlay); 471 if (rv != 0) { 472 printf("DTB overlay '%s' not compatible\n", fp->f_name); 473 continue; 474 } 475 printf("applying DTB overlay '%s'\n", fp->f_name); 476 next_fdtp_size = current_fdtp_size + fp->f_size; 477 next_fdtp = malloc(next_fdtp_size); 478 if (next_fdtp == NULL) { 479 /* 480 * Output warning, then move on to applying other 481 * overlays in case this one is simply too large. 482 */ 483 printf("failed to allocate memory for overlay base\n"); 484 continue; 485 } 486 rv = fdt_open_into(current_fdtp, next_fdtp, next_fdtp_size); 487 if (rv != 0) { 488 free(next_fdtp); 489 printf("failed to open base dtb into overlay base\n"); 490 continue; 491 } 492 /* Both overlay and new_fdtp may be modified in place */ 493 rv = fdt_overlay_apply(next_fdtp, overlay); 494 if (rv == 0) { 495 /* Rotate next -> current */ 496 if (current_fdtp != fdtp) 497 free(current_fdtp); 498 current_fdtp = next_fdtp; 499 current_fdtp_size = next_fdtp_size; 500 } else { 501 /* 502 * Assume here that the base we tried to apply on is 503 * either trashed or in an inconsistent state. Trying to 504 * load it might work, but it's better to discard it and 505 * play it safe. */ 506 free(next_fdtp); 507 printf("failed to apply overlay: %s\n", 508 fdt_strerror(rv)); 509 } 510 } 511 /* We could have failed to apply all overlays; then we do nothing */ 512 if (current_fdtp != fdtp) { 513 free(fdtp); 514 fdtp = current_fdtp; 515 fdtp_size = current_fdtp_size; 516 } 517 free(overlay); 518 } 519 520 int 521 fdt_setup_fdtp() 522 { 523 struct preloaded_file *bfp; 524 vm_offset_t va; 525 526 debugf("fdt_setup_fdtp()\n"); 527 528 /* If we already loaded a file, use it. */ 529 if ((bfp = file_findfile(NULL, "dtb")) != NULL) { 530 if (fdt_load_dtb(bfp->f_addr) == 0) { 531 printf("Using DTB from loaded file '%s'.\n", 532 bfp->f_name); 533 return (0); 534 } 535 } 536 537 /* If we were given the address of a valid blob in memory, use it. */ 538 if (fdt_to_load != NULL) { 539 if (fdt_load_dtb_addr(fdt_to_load) == 0) { 540 printf("Using DTB from memory address %p.\n", 541 fdt_to_load); 542 return (0); 543 } 544 } 545 546 if (fdt_platform_load_dtb() == 0) 547 return (0); 548 549 /* If there is a dtb compiled into the kernel, use it. */ 550 if ((va = fdt_find_static_dtb()) != 0) { 551 if (fdt_load_dtb(va) == 0) { 552 printf("Using DTB compiled into kernel.\n"); 553 return (0); 554 } 555 } 556 557 command_errmsg = "No device tree blob found!\n"; 558 return (1); 559 } 560 561 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \ 562 (cellbuf), (lim), (cellsize), 0); 563 564 /* Force using base 16 */ 565 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \ 566 (cellbuf), (lim), (cellsize), 16); 567 568 static int 569 _fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize, 570 uint8_t base) 571 { 572 const char *buf = str; 573 const char *end = str + strlen(str) - 2; 574 uint32_t *u32buf = NULL; 575 uint8_t *u8buf = NULL; 576 int cnt = 0; 577 578 if (cellsize == sizeof(uint32_t)) 579 u32buf = (uint32_t *)cellbuf; 580 else 581 u8buf = (uint8_t *)cellbuf; 582 583 if (lim == 0) 584 return (0); 585 586 while (buf < end) { 587 588 /* Skip white whitespace(s)/separators */ 589 while (!isxdigit(*buf) && buf < end) 590 buf++; 591 592 if (u32buf != NULL) 593 u32buf[cnt] = 594 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base)); 595 596 else 597 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base); 598 599 if (cnt + 1 <= lim - 1) 600 cnt++; 601 else 602 break; 603 buf++; 604 /* Find another number */ 605 while ((isxdigit(*buf) || *buf == 'x') && buf < end) 606 buf++; 607 } 608 return (cnt); 609 } 610 611 void 612 fdt_fixup_ethernet(const char *str, char *ethstr, int len) 613 { 614 uint8_t tmp_addr[6]; 615 616 /* Convert macaddr string into a vector of uints */ 617 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t)); 618 /* Set actual property to a value from vect */ 619 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr), 620 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t)); 621 } 622 623 void 624 fdt_fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq) 625 { 626 int lo, o = 0, o2, maxo = 0, depth; 627 const uint32_t zero = 0; 628 629 /* We want to modify every subnode of /cpus */ 630 o = fdt_path_offset(fdtp, "/cpus"); 631 if (o < 0) 632 return; 633 634 /* maxo should contain offset of node next to /cpus */ 635 depth = 0; 636 maxo = o; 637 while (depth != -1) 638 maxo = fdt_next_node(fdtp, maxo, &depth); 639 640 /* Find CPU frequency properties */ 641 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency", 642 &zero, sizeof(uint32_t)); 643 644 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero, 645 sizeof(uint32_t)); 646 647 lo = MIN(o, o2); 648 649 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) { 650 651 o = fdt_node_offset_by_prop_value(fdtp, lo, 652 "clock-frequency", &zero, sizeof(uint32_t)); 653 654 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency", 655 &zero, sizeof(uint32_t)); 656 657 /* We're only interested in /cpus subnode(s) */ 658 if (lo > maxo) 659 break; 660 661 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency", 662 (uint32_t)cpufreq); 663 664 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency", 665 (uint32_t)busfreq); 666 667 lo = MIN(o, o2); 668 } 669 } 670 671 #ifdef notyet 672 static int 673 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells) 674 { 675 int cells_in_tuple, i, tuples, tuple_size; 676 uint32_t cur_start, cur_size; 677 678 cells_in_tuple = (addr_cells + size_cells); 679 tuple_size = cells_in_tuple * sizeof(uint32_t); 680 tuples = len / tuple_size; 681 if (tuples == 0) 682 return (EINVAL); 683 684 for (i = 0; i < tuples; i++) { 685 if (addr_cells == 2) 686 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]); 687 else 688 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]); 689 690 if (size_cells == 2) 691 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]); 692 else 693 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]); 694 695 if (cur_size == 0) 696 return (EINVAL); 697 698 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n", 699 i, cur_start, cur_size); 700 } 701 return (0); 702 } 703 #endif 704 705 void 706 fdt_fixup_memory(struct fdt_mem_region *region, size_t num) 707 { 708 struct fdt_mem_region *curmr; 709 uint32_t addr_cells, size_cells; 710 uint32_t *addr_cellsp, *size_cellsp; 711 int err, i, len, memory, root; 712 size_t realmrno; 713 uint8_t *buf, *sb; 714 uint64_t rstart, rsize; 715 int reserved; 716 717 root = fdt_path_offset(fdtp, "/"); 718 if (root < 0) { 719 sprintf(command_errbuf, "Could not find root node !"); 720 return; 721 } 722 723 memory = fdt_path_offset(fdtp, "/memory"); 724 if (memory <= 0) { 725 /* Create proper '/memory' node. */ 726 memory = fdt_add_subnode(fdtp, root, "memory"); 727 if (memory <= 0) { 728 snprintf(command_errbuf, sizeof(command_errbuf), 729 "Could not fixup '/memory' " 730 "node, error code : %d!\n", memory); 731 return; 732 } 733 734 err = fdt_setprop(fdtp, memory, "device_type", "memory", 735 sizeof("memory")); 736 737 if (err < 0) 738 return; 739 } 740 741 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells", 742 NULL); 743 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL); 744 745 if (addr_cellsp == NULL || size_cellsp == NULL) { 746 snprintf(command_errbuf, sizeof(command_errbuf), 747 "Could not fixup '/memory' node : " 748 "%s %s property not found in root node!\n", 749 (!addr_cellsp) ? "#address-cells" : "", 750 (!size_cellsp) ? "#size-cells" : ""); 751 return; 752 } 753 754 addr_cells = fdt32_to_cpu(*addr_cellsp); 755 size_cells = fdt32_to_cpu(*size_cellsp); 756 757 /* 758 * Convert memreserve data to memreserve property 759 * Check if property already exists 760 */ 761 reserved = fdt_num_mem_rsv(fdtp); 762 if (reserved && 763 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) { 764 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t); 765 sb = buf = (uint8_t *)malloc(len); 766 if (!buf) 767 return; 768 769 bzero(buf, len); 770 771 for (i = 0; i < reserved; i++) { 772 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize)) 773 break; 774 if (rsize) { 775 /* Ensure endianness, and put cells into a buffer */ 776 if (addr_cells == 2) 777 *(uint64_t *)buf = 778 cpu_to_fdt64(rstart); 779 else 780 *(uint32_t *)buf = 781 cpu_to_fdt32(rstart); 782 783 buf += sizeof(uint32_t) * addr_cells; 784 if (size_cells == 2) 785 *(uint64_t *)buf = 786 cpu_to_fdt64(rsize); 787 else 788 *(uint32_t *)buf = 789 cpu_to_fdt32(rsize); 790 791 buf += sizeof(uint32_t) * size_cells; 792 } 793 } 794 795 /* Set property */ 796 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0) 797 printf("Could not fixup 'memreserve' property.\n"); 798 799 free(sb); 800 } 801 802 /* Count valid memory regions entries in sysinfo. */ 803 realmrno = num; 804 for (i = 0; i < num; i++) 805 if (region[i].start == 0 && region[i].size == 0) 806 realmrno--; 807 808 if (realmrno == 0) { 809 sprintf(command_errbuf, "Could not fixup '/memory' node : " 810 "sysinfo doesn't contain valid memory regions info!\n"); 811 return; 812 } 813 814 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t); 815 sb = buf = (uint8_t *)malloc(len); 816 if (!buf) 817 return; 818 819 bzero(buf, len); 820 821 for (i = 0; i < num; i++) { 822 curmr = ®ion[i]; 823 if (curmr->size != 0) { 824 /* Ensure endianness, and put cells into a buffer */ 825 if (addr_cells == 2) 826 *(uint64_t *)buf = 827 cpu_to_fdt64(curmr->start); 828 else 829 *(uint32_t *)buf = 830 cpu_to_fdt32(curmr->start); 831 832 buf += sizeof(uint32_t) * addr_cells; 833 if (size_cells == 2) 834 *(uint64_t *)buf = 835 cpu_to_fdt64(curmr->size); 836 else 837 *(uint32_t *)buf = 838 cpu_to_fdt32(curmr->size); 839 840 buf += sizeof(uint32_t) * size_cells; 841 } 842 } 843 844 /* Set property */ 845 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0) 846 sprintf(command_errbuf, "Could not fixup '/memory' node.\n"); 847 848 free(sb); 849 } 850 851 void 852 fdt_fixup_stdout(const char *str) 853 { 854 char *ptr; 855 int serialno; 856 int len, no, sero; 857 const struct fdt_property *prop; 858 char *tmp[10]; 859 860 ptr = (char *)str + strlen(str) - 1; 861 while (ptr > str && isdigit(*(str - 1))) 862 str--; 863 864 if (ptr == str) 865 return; 866 867 serialno = (int)strtol(ptr, NULL, 0); 868 no = fdt_path_offset(fdtp, "/chosen"); 869 if (no < 0) 870 return; 871 872 prop = fdt_get_property(fdtp, no, "stdout", &len); 873 874 /* If /chosen/stdout does not extist, create it */ 875 if (prop == NULL || (prop != NULL && len == 0)) { 876 877 bzero(tmp, 10 * sizeof(char)); 878 strcpy((char *)&tmp, "serial"); 879 if (strlen(ptr) > 3) 880 /* Serial number too long */ 881 return; 882 883 strncpy((char *)tmp + 6, ptr, 3); 884 sero = fdt_path_offset(fdtp, (const char *)tmp); 885 if (sero < 0) 886 /* 887 * If serial device we're trying to assign 888 * stdout to doesn't exist in DT -- return. 889 */ 890 return; 891 892 fdt_setprop(fdtp, no, "stdout", &tmp, 893 strlen((char *)&tmp) + 1); 894 fdt_setprop(fdtp, no, "stdin", &tmp, 895 strlen((char *)&tmp) + 1); 896 } 897 } 898 899 void 900 fdt_load_dtb_overlays(const char *extras) 901 { 902 const char *s; 903 904 /* Any extra overlays supplied by pre-loader environment */ 905 if (extras != NULL && *extras != '\0') { 906 printf("Loading DTB overlays: '%s'\n", extras); 907 fdt_load_dtb_overlays_string(extras); 908 } 909 910 /* Any overlays supplied by loader environment */ 911 s = getenv("fdt_overlays"); 912 if (s != NULL && *s != '\0') { 913 printf("Loading DTB overlays: '%s'\n", s); 914 fdt_load_dtb_overlays_string(s); 915 } 916 } 917 918 /* 919 * Locate the blob, fix it up and return its location. 920 */ 921 static int 922 fdt_fixup(void) 923 { 924 int chosen, len; 925 926 len = 0; 927 928 debugf("fdt_fixup()\n"); 929 930 if (fdtp == NULL && fdt_setup_fdtp() != 0) 931 return (0); 932 933 /* Create /chosen node (if not exists) */ 934 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) == 935 -FDT_ERR_NOTFOUND) 936 chosen = fdt_add_subnode(fdtp, 0, "chosen"); 937 938 /* Value assigned to fixup-applied does not matter. */ 939 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL)) 940 return (1); 941 942 fdt_platform_fixups(); 943 944 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0); 945 return (1); 946 } 947 948 /* 949 * Copy DTB blob to specified location and return size 950 */ 951 int 952 fdt_copy(vm_offset_t va) 953 { 954 int err; 955 debugf("fdt_copy va 0x%08x\n", va); 956 if (fdtp == NULL) { 957 err = fdt_setup_fdtp(); 958 if (err) { 959 printf("No valid device tree blob found!\n"); 960 return (0); 961 } 962 } 963 964 if (fdt_fixup() == 0) 965 return (0); 966 967 if (fdtp_va != 0) { 968 /* Overwrite the FDT with the fixed version. */ 969 /* XXX Is this really appropriate? */ 970 COPYIN(fdtp, fdtp_va, fdtp_size); 971 } 972 COPYIN(fdtp, va, fdtp_size); 973 return (fdtp_size); 974 } 975 976 977 978 int 979 command_fdt_internal(int argc, char *argv[]) 980 { 981 cmdf_t *cmdh; 982 int flags; 983 char *cmd; 984 int i, err; 985 986 if (argc < 2) { 987 command_errmsg = "usage is 'fdt <command> [<args>]"; 988 return (CMD_ERROR); 989 } 990 991 /* 992 * Validate fdt <command>. 993 */ 994 cmd = strdup(argv[1]); 995 i = 0; 996 cmdh = NULL; 997 while (!(commands[i].name == NULL)) { 998 if (strcmp(cmd, commands[i].name) == 0) { 999 /* found it */ 1000 cmdh = commands[i].handler; 1001 flags = commands[i].flags; 1002 break; 1003 } 1004 i++; 1005 } 1006 if (cmdh == NULL) { 1007 command_errmsg = "unknown command"; 1008 return (CMD_ERROR); 1009 } 1010 1011 if (flags & CMD_REQUIRES_BLOB) { 1012 /* 1013 * Check if uboot env vars were parsed already. If not, do it now. 1014 */ 1015 if (fdt_fixup() == 0) 1016 return (CMD_ERROR); 1017 } 1018 1019 /* 1020 * Call command handler. 1021 */ 1022 err = (*cmdh)(argc, argv); 1023 1024 return (err); 1025 } 1026 1027 static int 1028 fdt_cmd_addr(int argc, char *argv[]) 1029 { 1030 struct preloaded_file *fp; 1031 struct fdt_header *hdr; 1032 const char *addr; 1033 char *cp; 1034 1035 fdt_to_load = NULL; 1036 1037 if (argc > 2) 1038 addr = argv[2]; 1039 else { 1040 sprintf(command_errbuf, "no address specified"); 1041 return (CMD_ERROR); 1042 } 1043 1044 hdr = (struct fdt_header *)strtoul(addr, &cp, 16); 1045 if (cp == addr) { 1046 snprintf(command_errbuf, sizeof(command_errbuf), 1047 "Invalid address: %s", addr); 1048 return (CMD_ERROR); 1049 } 1050 1051 while ((fp = file_findfile(NULL, "dtb")) != NULL) { 1052 file_discard(fp); 1053 } 1054 1055 fdt_to_load = hdr; 1056 return (CMD_OK); 1057 } 1058 1059 static int 1060 fdt_cmd_cd(int argc, char *argv[]) 1061 { 1062 char *path; 1063 char tmp[FDT_CWD_LEN]; 1064 int len, o; 1065 1066 path = (argc > 2) ? argv[2] : "/"; 1067 1068 if (path[0] == '/') { 1069 len = strlen(path); 1070 if (len >= FDT_CWD_LEN) 1071 goto fail; 1072 } else { 1073 /* Handle path specification relative to cwd */ 1074 len = strlen(cwd) + strlen(path) + 1; 1075 if (len >= FDT_CWD_LEN) 1076 goto fail; 1077 1078 strcpy(tmp, cwd); 1079 strcat(tmp, "/"); 1080 strcat(tmp, path); 1081 path = tmp; 1082 } 1083 1084 o = fdt_path_offset(fdtp, path); 1085 if (o < 0) { 1086 snprintf(command_errbuf, sizeof(command_errbuf), 1087 "could not find node: '%s'", path); 1088 return (CMD_ERROR); 1089 } 1090 1091 strcpy(cwd, path); 1092 return (CMD_OK); 1093 1094 fail: 1095 snprintf(command_errbuf, sizeof(command_errbuf), 1096 "path too long: %d, max allowed: %d", len, FDT_CWD_LEN - 1); 1097 return (CMD_ERROR); 1098 } 1099 1100 static int 1101 fdt_cmd_hdr(int argc __unused, char *argv[] __unused) 1102 { 1103 char line[80]; 1104 int ver; 1105 1106 if (fdtp == NULL) { 1107 command_errmsg = "no device tree blob pointer?!"; 1108 return (CMD_ERROR); 1109 } 1110 1111 ver = fdt_version(fdtp); 1112 pager_open(); 1113 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp); 1114 if (pager_output(line)) 1115 goto out; 1116 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp)); 1117 if (pager_output(line)) 1118 goto out; 1119 sprintf(line, " size = %d\n", fdt_totalsize(fdtp)); 1120 if (pager_output(line)) 1121 goto out; 1122 sprintf(line, " off_dt_struct = 0x%08x\n", 1123 fdt_off_dt_struct(fdtp)); 1124 if (pager_output(line)) 1125 goto out; 1126 sprintf(line, " off_dt_strings = 0x%08x\n", 1127 fdt_off_dt_strings(fdtp)); 1128 if (pager_output(line)) 1129 goto out; 1130 sprintf(line, " off_mem_rsvmap = 0x%08x\n", 1131 fdt_off_mem_rsvmap(fdtp)); 1132 if (pager_output(line)) 1133 goto out; 1134 sprintf(line, " version = %d\n", ver); 1135 if (pager_output(line)) 1136 goto out; 1137 sprintf(line, " last compatible version = %d\n", 1138 fdt_last_comp_version(fdtp)); 1139 if (pager_output(line)) 1140 goto out; 1141 if (ver >= 2) { 1142 sprintf(line, " boot_cpuid = %d\n", 1143 fdt_boot_cpuid_phys(fdtp)); 1144 if (pager_output(line)) 1145 goto out; 1146 } 1147 if (ver >= 3) { 1148 sprintf(line, " size_dt_strings = %d\n", 1149 fdt_size_dt_strings(fdtp)); 1150 if (pager_output(line)) 1151 goto out; 1152 } 1153 if (ver >= 17) { 1154 sprintf(line, " size_dt_struct = %d\n", 1155 fdt_size_dt_struct(fdtp)); 1156 if (pager_output(line)) 1157 goto out; 1158 } 1159 out: 1160 pager_close(); 1161 1162 return (CMD_OK); 1163 } 1164 1165 static int 1166 fdt_cmd_ls(int argc, char *argv[]) 1167 { 1168 const char *prevname[FDT_MAX_DEPTH] = { NULL }; 1169 const char *name; 1170 char *path; 1171 int i, o, depth; 1172 1173 path = (argc > 2) ? argv[2] : NULL; 1174 if (path == NULL) 1175 path = cwd; 1176 1177 o = fdt_path_offset(fdtp, path); 1178 if (o < 0) { 1179 snprintf(command_errbuf, sizeof(command_errbuf), 1180 "could not find node: '%s'", path); 1181 return (CMD_ERROR); 1182 } 1183 1184 for (depth = 0; 1185 (o >= 0) && (depth >= 0); 1186 o = fdt_next_node(fdtp, o, &depth)) { 1187 1188 name = fdt_get_name(fdtp, o, NULL); 1189 1190 if (depth > FDT_MAX_DEPTH) { 1191 printf("max depth exceeded: %d\n", depth); 1192 continue; 1193 } 1194 1195 prevname[depth] = name; 1196 1197 /* Skip root (i = 1) when printing devices */ 1198 for (i = 1; i <= depth; i++) { 1199 if (prevname[i] == NULL) 1200 break; 1201 1202 if (strcmp(cwd, "/") == 0) 1203 printf("/"); 1204 printf("%s", prevname[i]); 1205 } 1206 printf("\n"); 1207 } 1208 1209 return (CMD_OK); 1210 } 1211 1212 static __inline int 1213 isprint(int c) 1214 { 1215 1216 return (c >= ' ' && c <= 0x7e); 1217 } 1218 1219 static int 1220 fdt_isprint(const void *data, int len, int *count) 1221 { 1222 const char *d; 1223 char ch; 1224 int yesno, i; 1225 1226 if (len == 0) 1227 return (0); 1228 1229 d = (const char *)data; 1230 if (d[len - 1] != '\0') 1231 return (0); 1232 1233 *count = 0; 1234 yesno = 1; 1235 for (i = 0; i < len; i++) { 1236 ch = *(d + i); 1237 if (isprint(ch) || (ch == '\0' && i > 0)) { 1238 /* Count strings */ 1239 if (ch == '\0') 1240 (*count)++; 1241 continue; 1242 } 1243 1244 yesno = 0; 1245 break; 1246 } 1247 1248 return (yesno); 1249 } 1250 1251 static int 1252 fdt_data_str(const void *data, int len, int count, char **buf) 1253 { 1254 char *b, *tmp; 1255 const char *d; 1256 int buf_len, i, l; 1257 1258 /* 1259 * Calculate the length for the string and allocate memory. 1260 * 1261 * Note that 'len' already includes at least one terminator. 1262 */ 1263 buf_len = len; 1264 if (count > 1) { 1265 /* 1266 * Each token had already a terminator buried in 'len', but we 1267 * only need one eventually, don't count space for these. 1268 */ 1269 buf_len -= count - 1; 1270 1271 /* Each consecutive token requires a ", " separator. */ 1272 buf_len += count * 2; 1273 } 1274 1275 /* Add some space for surrounding double quotes. */ 1276 buf_len += count * 2; 1277 1278 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */ 1279 b = (char *)malloc(buf_len); 1280 tmp = (char *)malloc(buf_len); 1281 if (b == NULL) 1282 goto error; 1283 1284 if (tmp == NULL) { 1285 free(b); 1286 goto error; 1287 } 1288 1289 b[0] = '\0'; 1290 1291 /* 1292 * Now that we have space, format the string. 1293 */ 1294 i = 0; 1295 do { 1296 d = (const char *)data + i; 1297 l = strlen(d) + 1; 1298 1299 sprintf(tmp, "\"%s\"%s", d, 1300 (i + l) < len ? ", " : ""); 1301 strcat(b, tmp); 1302 1303 i += l; 1304 1305 } while (i < len); 1306 *buf = b; 1307 1308 free(tmp); 1309 1310 return (0); 1311 error: 1312 return (1); 1313 } 1314 1315 static int 1316 fdt_data_cell(const void *data, int len, char **buf) 1317 { 1318 char *b, *tmp; 1319 const uint32_t *c; 1320 int count, i, l; 1321 1322 /* Number of cells */ 1323 count = len / 4; 1324 1325 /* 1326 * Calculate the length for the string and allocate memory. 1327 */ 1328 1329 /* Each byte translates to 2 output characters */ 1330 l = len * 2; 1331 if (count > 1) { 1332 /* Each consecutive cell requires a " " separator. */ 1333 l += (count - 1) * 1; 1334 } 1335 /* Each cell will have a "0x" prefix */ 1336 l += count * 2; 1337 /* Space for surrounding <> and terminator */ 1338 l += 3; 1339 1340 b = (char *)malloc(l); 1341 tmp = (char *)malloc(l); 1342 if (b == NULL) 1343 goto error; 1344 1345 if (tmp == NULL) { 1346 free(b); 1347 goto error; 1348 } 1349 1350 b[0] = '\0'; 1351 strcat(b, "<"); 1352 1353 for (i = 0; i < len; i += 4) { 1354 c = (const uint32_t *)((const uint8_t *)data + i); 1355 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c), 1356 i < (len - 4) ? " " : ""); 1357 strcat(b, tmp); 1358 } 1359 strcat(b, ">"); 1360 *buf = b; 1361 1362 free(tmp); 1363 1364 return (0); 1365 error: 1366 return (1); 1367 } 1368 1369 static int 1370 fdt_data_bytes(const void *data, int len, char **buf) 1371 { 1372 char *b, *tmp; 1373 const char *d; 1374 int i, l; 1375 1376 /* 1377 * Calculate the length for the string and allocate memory. 1378 */ 1379 1380 /* Each byte translates to 2 output characters */ 1381 l = len * 2; 1382 if (len > 1) 1383 /* Each consecutive byte requires a " " separator. */ 1384 l += (len - 1) * 1; 1385 /* Each byte will have a "0x" prefix */ 1386 l += len * 2; 1387 /* Space for surrounding [] and terminator. */ 1388 l += 3; 1389 1390 b = (char *)malloc(l); 1391 tmp = (char *)malloc(l); 1392 if (b == NULL) 1393 goto error; 1394 1395 if (tmp == NULL) { 1396 free(b); 1397 goto error; 1398 } 1399 1400 b[0] = '\0'; 1401 strcat(b, "["); 1402 1403 for (i = 0, d = data; i < len; i++) { 1404 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : ""); 1405 strcat(b, tmp); 1406 } 1407 strcat(b, "]"); 1408 *buf = b; 1409 1410 free(tmp); 1411 1412 return (0); 1413 error: 1414 return (1); 1415 } 1416 1417 static int 1418 fdt_data_fmt(const void *data, int len, char **buf) 1419 { 1420 int count; 1421 1422 if (len == 0) { 1423 *buf = NULL; 1424 return (1); 1425 } 1426 1427 if (fdt_isprint(data, len, &count)) 1428 return (fdt_data_str(data, len, count, buf)); 1429 1430 else if ((len % 4) == 0) 1431 return (fdt_data_cell(data, len, buf)); 1432 1433 else 1434 return (fdt_data_bytes(data, len, buf)); 1435 } 1436 1437 static int 1438 fdt_prop(int offset) 1439 { 1440 char *line, *buf; 1441 const struct fdt_property *prop; 1442 const char *name; 1443 const void *data; 1444 int len, rv; 1445 1446 line = NULL; 1447 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop)); 1448 if (prop == NULL) 1449 return (1); 1450 1451 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff)); 1452 len = fdt32_to_cpu(prop->len); 1453 1454 rv = 0; 1455 buf = NULL; 1456 if (len == 0) { 1457 /* Property without value */ 1458 line = (char *)malloc(strlen(name) + 2); 1459 if (line == NULL) { 1460 rv = 2; 1461 goto out2; 1462 } 1463 sprintf(line, "%s\n", name); 1464 goto out1; 1465 } 1466 1467 /* 1468 * Process property with value 1469 */ 1470 data = prop->data; 1471 1472 if (fdt_data_fmt(data, len, &buf) != 0) { 1473 rv = 3; 1474 goto out2; 1475 } 1476 1477 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) + 1478 strlen(buf) + 2); 1479 if (line == NULL) { 1480 sprintf(command_errbuf, "could not allocate space for string"); 1481 rv = 4; 1482 goto out2; 1483 } 1484 1485 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf); 1486 1487 out1: 1488 pager_open(); 1489 pager_output(line); 1490 pager_close(); 1491 1492 out2: 1493 if (buf) 1494 free(buf); 1495 1496 if (line) 1497 free(line); 1498 1499 return (rv); 1500 } 1501 1502 static int 1503 fdt_modprop(int nodeoff, char *propname, void *value, char mode) 1504 { 1505 uint32_t cells[100]; 1506 const char *buf; 1507 int len, rv; 1508 const struct fdt_property *p; 1509 1510 p = fdt_get_property(fdtp, nodeoff, propname, NULL); 1511 1512 if (p != NULL) { 1513 if (mode == 1) { 1514 /* Adding inexistant value in mode 1 is forbidden */ 1515 sprintf(command_errbuf, "property already exists!"); 1516 return (CMD_ERROR); 1517 } 1518 } else if (mode == 0) { 1519 sprintf(command_errbuf, "property does not exist!"); 1520 return (CMD_ERROR); 1521 } 1522 len = strlen(value); 1523 rv = 0; 1524 buf = value; 1525 1526 switch (*buf) { 1527 case '&': 1528 /* phandles */ 1529 break; 1530 case '<': 1531 /* Data cells */ 1532 len = fdt_strtovect(buf, (void *)&cells, 100, 1533 sizeof(uint32_t)); 1534 1535 rv = fdt_setprop(fdtp, nodeoff, propname, &cells, 1536 len * sizeof(uint32_t)); 1537 break; 1538 case '[': 1539 /* Data bytes */ 1540 len = fdt_strtovect(buf, (void *)&cells, 100, 1541 sizeof(uint8_t)); 1542 1543 rv = fdt_setprop(fdtp, nodeoff, propname, &cells, 1544 len * sizeof(uint8_t)); 1545 break; 1546 case '"': 1547 default: 1548 /* Default -- string */ 1549 rv = fdt_setprop_string(fdtp, nodeoff, propname, value); 1550 break; 1551 } 1552 1553 if (rv != 0) { 1554 if (rv == -FDT_ERR_NOSPACE) 1555 sprintf(command_errbuf, 1556 "Device tree blob is too small!\n"); 1557 else 1558 sprintf(command_errbuf, 1559 "Could not add/modify property!\n"); 1560 } 1561 return (rv); 1562 } 1563 1564 /* Merge strings from argv into a single string */ 1565 static int 1566 fdt_merge_strings(int argc, char *argv[], int start, char **buffer) 1567 { 1568 char *buf; 1569 int i, idx, sz; 1570 1571 *buffer = NULL; 1572 sz = 0; 1573 1574 for (i = start; i < argc; i++) 1575 sz += strlen(argv[i]); 1576 1577 /* Additional bytes for whitespaces between args */ 1578 sz += argc - start; 1579 1580 buf = (char *)malloc(sizeof(char) * sz); 1581 if (buf == NULL) { 1582 sprintf(command_errbuf, "could not allocate space " 1583 "for string"); 1584 return (1); 1585 } 1586 bzero(buf, sizeof(char) * sz); 1587 1588 idx = 0; 1589 for (i = start, idx = 0; i < argc; i++) { 1590 strcpy(buf + idx, argv[i]); 1591 idx += strlen(argv[i]); 1592 buf[idx] = ' '; 1593 idx++; 1594 } 1595 buf[sz - 1] = '\0'; 1596 *buffer = buf; 1597 return (0); 1598 } 1599 1600 /* Extract offset and name of node/property from a given path */ 1601 static int 1602 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff) 1603 { 1604 int o; 1605 char *path = *pathp, *name = NULL, *subpath = NULL; 1606 1607 subpath = strrchr(path, '/'); 1608 if (subpath == NULL) { 1609 o = fdt_path_offset(fdtp, cwd); 1610 name = path; 1611 path = (char *)&cwd; 1612 } else { 1613 *subpath = '\0'; 1614 if (strlen(path) == 0) 1615 path = cwd; 1616 1617 name = subpath + 1; 1618 o = fdt_path_offset(fdtp, path); 1619 } 1620 1621 if (strlen(name) == 0) { 1622 sprintf(command_errbuf, "name not specified"); 1623 return (1); 1624 } 1625 if (o < 0) { 1626 snprintf(command_errbuf, sizeof(command_errbuf), 1627 "could not find node: '%s'", path); 1628 return (1); 1629 } 1630 *namep = name; 1631 *nodeoff = o; 1632 *pathp = path; 1633 return (0); 1634 } 1635 1636 static int 1637 fdt_cmd_prop(int argc, char *argv[]) 1638 { 1639 char *path, *propname, *value; 1640 int o, next, depth, rv; 1641 uint32_t tag; 1642 1643 path = (argc > 2) ? argv[2] : NULL; 1644 1645 value = NULL; 1646 1647 if (argc > 3) { 1648 /* Merge property value strings into one */ 1649 if (fdt_merge_strings(argc, argv, 3, &value) != 0) 1650 return (CMD_ERROR); 1651 } else 1652 value = NULL; 1653 1654 if (path == NULL) 1655 path = cwd; 1656 1657 rv = CMD_OK; 1658 1659 if (value) { 1660 /* If value is specified -- try to modify prop. */ 1661 if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1662 return (CMD_ERROR); 1663 1664 rv = fdt_modprop(o, propname, value, 0); 1665 if (rv) 1666 return (CMD_ERROR); 1667 return (CMD_OK); 1668 1669 } 1670 /* User wants to display properties */ 1671 o = fdt_path_offset(fdtp, path); 1672 1673 if (o < 0) { 1674 snprintf(command_errbuf, sizeof(command_errbuf), 1675 "could not find node: '%s'", path); 1676 rv = CMD_ERROR; 1677 goto out; 1678 } 1679 1680 depth = 0; 1681 while (depth >= 0) { 1682 tag = fdt_next_tag(fdtp, o, &next); 1683 switch (tag) { 1684 case FDT_NOP: 1685 break; 1686 case FDT_PROP: 1687 if (depth > 1) 1688 /* Don't process properties of nested nodes */ 1689 break; 1690 1691 if (fdt_prop(o) != 0) { 1692 sprintf(command_errbuf, "could not process " 1693 "property"); 1694 rv = CMD_ERROR; 1695 goto out; 1696 } 1697 break; 1698 case FDT_BEGIN_NODE: 1699 depth++; 1700 if (depth > FDT_MAX_DEPTH) { 1701 printf("warning: nesting too deep: %d\n", 1702 depth); 1703 goto out; 1704 } 1705 break; 1706 case FDT_END_NODE: 1707 depth--; 1708 if (depth == 0) 1709 /* 1710 * This is the end of our starting node, force 1711 * the loop finish. 1712 */ 1713 depth--; 1714 break; 1715 } 1716 o = next; 1717 } 1718 out: 1719 return (rv); 1720 } 1721 1722 static int 1723 fdt_cmd_mkprop(int argc, char *argv[]) 1724 { 1725 int o; 1726 char *path, *propname, *value; 1727 1728 path = (argc > 2) ? argv[2] : NULL; 1729 1730 value = NULL; 1731 1732 if (argc > 3) { 1733 /* Merge property value strings into one */ 1734 if (fdt_merge_strings(argc, argv, 3, &value) != 0) 1735 return (CMD_ERROR); 1736 } else 1737 value = NULL; 1738 1739 if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1740 return (CMD_ERROR); 1741 1742 if (fdt_modprop(o, propname, value, 1)) 1743 return (CMD_ERROR); 1744 1745 return (CMD_OK); 1746 } 1747 1748 static int 1749 fdt_cmd_rm(int argc, char *argv[]) 1750 { 1751 int o, rv; 1752 char *path = NULL, *propname; 1753 1754 if (argc > 2) 1755 path = argv[2]; 1756 else { 1757 sprintf(command_errbuf, "no node/property name specified"); 1758 return (CMD_ERROR); 1759 } 1760 1761 o = fdt_path_offset(fdtp, path); 1762 if (o < 0) { 1763 /* If node not found -- try to find & delete property */ 1764 if (fdt_extract_nameloc(&path, &propname, &o) != 0) 1765 return (CMD_ERROR); 1766 1767 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) { 1768 snprintf(command_errbuf, sizeof(command_errbuf), 1769 "could not delete %s\n", 1770 (rv == -FDT_ERR_NOTFOUND) ? 1771 "(property/node does not exist)" : ""); 1772 return (CMD_ERROR); 1773 1774 } else 1775 return (CMD_OK); 1776 } 1777 /* If node exists -- remove node */ 1778 rv = fdt_del_node(fdtp, o); 1779 if (rv) { 1780 sprintf(command_errbuf, "could not delete node"); 1781 return (CMD_ERROR); 1782 } 1783 return (CMD_OK); 1784 } 1785 1786 static int 1787 fdt_cmd_mknode(int argc, char *argv[]) 1788 { 1789 int o, rv; 1790 char *path = NULL, *nodename = NULL; 1791 1792 if (argc > 2) 1793 path = argv[2]; 1794 else { 1795 sprintf(command_errbuf, "no node name specified"); 1796 return (CMD_ERROR); 1797 } 1798 1799 if (fdt_extract_nameloc(&path, &nodename, &o) != 0) 1800 return (CMD_ERROR); 1801 1802 rv = fdt_add_subnode(fdtp, o, nodename); 1803 1804 if (rv < 0) { 1805 if (rv == -FDT_ERR_NOSPACE) 1806 sprintf(command_errbuf, 1807 "Device tree blob is too small!\n"); 1808 else 1809 sprintf(command_errbuf, 1810 "Could not add node!\n"); 1811 return (CMD_ERROR); 1812 } 1813 return (CMD_OK); 1814 } 1815 1816 static int 1817 fdt_cmd_pwd(int argc, char *argv[]) 1818 { 1819 char line[FDT_CWD_LEN]; 1820 1821 pager_open(); 1822 sprintf(line, "%s\n", cwd); 1823 pager_output(line); 1824 pager_close(); 1825 return (CMD_OK); 1826 } 1827 1828 static int 1829 fdt_cmd_mres(int argc, char *argv[]) 1830 { 1831 uint64_t start, size; 1832 int i, total; 1833 char line[80]; 1834 1835 pager_open(); 1836 total = fdt_num_mem_rsv(fdtp); 1837 if (total > 0) { 1838 if (pager_output("Reserved memory regions:\n")) 1839 goto out; 1840 for (i = 0; i < total; i++) { 1841 fdt_get_mem_rsv(fdtp, i, &start, &size); 1842 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n", 1843 i, start, size); 1844 if (pager_output(line)) 1845 goto out; 1846 } 1847 } else 1848 pager_output("No reserved memory regions\n"); 1849 out: 1850 pager_close(); 1851 1852 return (CMD_OK); 1853 } 1854 1855 static int 1856 fdt_cmd_nyi(int argc, char *argv[]) 1857 { 1858 1859 printf("command not yet implemented\n"); 1860 return (CMD_ERROR); 1861 } 1862