rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 Brian Swetland |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include "devicetree.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <errno.h> |
| 31 | |
| 32 | void error(const char *msg) { |
| 33 | printf("error: %s\n", msg); |
| 34 | }; |
| 35 | |
| 36 | void indent(u32 n) { |
| 37 | while (n-- > 0) |
| 38 | putchar(' '); |
| 39 | } |
| 40 | |
| 41 | void hexdump(u8 *data, u32 count) { |
| 42 | while (count-- > 0) |
| 43 | printf("%02x ", *data++); |
| 44 | } |
| 45 | |
| 46 | int loadfile(const char *fn, dt_slice_t *out) { |
| 47 | void *data = NULL; |
| 48 | off_t end; |
| 49 | int fd; |
| 50 | if ((fd = open(fn, O_RDONLY)) < 0) |
| 51 | return -1; |
| 52 | if ((end = lseek(fd, 0, SEEK_END)) == -1) |
| 53 | goto oops; |
| 54 | if (lseek(fd, 0, SEEK_SET)) |
| 55 | goto oops; |
| 56 | if ((data = malloc(end + 1)) == NULL) |
| 57 | goto oops; |
| 58 | if ((read(fd, data, end) != end)) |
| 59 | goto oops; |
| 60 | out->data = data; |
| 61 | out->size = end; |
| 62 | close(fd); |
| 63 | return 0; |
| 64 | oops: |
| 65 | free(data); |
| 66 | close(fd); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | static int _depth = 0; |
| 71 | |
| 72 | int node_cb(int depth, const char *name, void *cookie) { |
| 73 | _depth = depth; |
| 74 | indent(depth*2); |
| 75 | printf("node: '%s'\n", name); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int prop_cb(const char *name, u8 *data, u32 size, void *cookie) { |
| 80 | indent(_depth * 2 + 2); |
| 81 | printf("prop '%s' sz=%d\n", name, size); |
| 82 | indent(_depth * 2 + 2); |
| 83 | printf("data "); |
| 84 | hexdump(data, size); |
| 85 | printf("\n"); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | int main(int argc, char **argv) { |
| 90 | devicetree_t dt; |
| 91 | dt_slice_t s; |
| 92 | |
| 93 | dt.error = error; |
| 94 | |
| 95 | if (argc != 2) |
| 96 | return -1; |
| 97 | if (loadfile(argv[1], &s)) |
| 98 | return -1; |
| 99 | if (dt_init(&dt, s.data, s.size)) |
| 100 | return -1; |
| 101 | |
| 102 | printf("magic %x\n", dt.hdr.magic); |
| 103 | printf("size %d\n", dt.hdr.size); |
| 104 | printf("off_struct %d (%d)\n", dt.hdr.off_struct, dt.hdr.sz_struct); |
| 105 | printf("off_strings %d (%d)\n", dt.hdr.off_strings, dt.hdr.sz_strings); |
| 106 | printf("version %d (min %d)\n", dt.hdr.version, dt.hdr.version_compat); |
| 107 | |
| 108 | dt_walk(&dt, node_cb, prop_cb, NULL); |
| 109 | return 0; |
| 110 | } |
| 111 | |