| 1 | /* |
| 2 | Copyright (c) 2016 Raspberry Pi (Trading) Ltd. |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <libfdt.h> |
| 31 | |
| 32 | #include "dtoverlay.h" |
| 33 | |
| 34 | static void usage(void) |
| 35 | { |
| 36 | printf("Usage:\n" ); |
| 37 | printf(" dtmerge [<options] <base dtb> <merged dtb> - [param=value] ...\n" ); |
| 38 | printf(" to apply a parameter to the base dtb (like dtparam)\n" ); |
| 39 | printf(" dtmerge [<options] <base dtb> <merged dtb> <overlay dtb> [param=value] ...\n" ); |
| 40 | printf(" to apply an overlay with parameters (like dtoverlay)\n" ); |
| 41 | printf(" where <options> is any of:\n" ); |
| 42 | printf(" -d Enable debug output\n" ); |
| 43 | printf(" -h Show this help message\n" ); |
| 44 | exit(1); |
| 45 | } |
| 46 | |
| 47 | int main(int argc, char **argv) |
| 48 | { |
| 49 | const char *base_file; |
| 50 | const char *merged_file; |
| 51 | const char *overlay_file; |
| 52 | DTBLOB_T *base_dtb; |
| 53 | DTBLOB_T *overlay_dtb; |
| 54 | int err; |
| 55 | int argn = 1; |
| 56 | int max_dtb_size = 100000; |
| 57 | |
| 58 | while ((argn < argc) && (argv[argn][0] == '-')) |
| 59 | { |
| 60 | const char *arg = argv[argn++]; |
| 61 | if ((strcmp(arg, "-d" ) == 0) || |
| 62 | (strcmp(arg, "--debug" ) == 0)) |
| 63 | dtoverlay_enable_debug(1); |
| 64 | else if ((strcmp(arg, "-h" ) == 0) || |
| 65 | (strcmp(arg, "--help" ) == 0)) |
| 66 | usage(); |
| 67 | else |
| 68 | { |
| 69 | printf("* Unknown option '%s'\n" , arg); |
| 70 | usage(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (argc < (argn + 3)) |
| 75 | { |
| 76 | usage(); |
| 77 | } |
| 78 | |
| 79 | base_file = argv[argn++]; |
| 80 | merged_file = argv[argn++]; |
| 81 | overlay_file = argv[argn++]; |
| 82 | |
| 83 | base_dtb = dtoverlay_load_dtb(base_file, max_dtb_size); |
| 84 | if (!base_dtb) |
| 85 | { |
| 86 | printf("* failed to load '%s'\n" , base_file); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | err = dtoverlay_set_synonym(base_dtb, "i2c" , "i2c0" ); |
| 91 | err = dtoverlay_set_synonym(base_dtb, "i2c_arm" , "i2c0" ); |
| 92 | err = dtoverlay_set_synonym(base_dtb, "i2c_vc" , "i2c1" ); |
| 93 | err = dtoverlay_set_synonym(base_dtb, "i2c_baudrate" , "i2c0_baudrate" ); |
| 94 | err = dtoverlay_set_synonym(base_dtb, "i2c_arm_baudrate" , "i2c0_baudrate" ); |
| 95 | err = dtoverlay_set_synonym(base_dtb, "i2c_vc_baudrate" , "i2c1_baudrate" ); |
| 96 | |
| 97 | if (strcmp(overlay_file, "-" ) == 0) |
| 98 | { |
| 99 | overlay_dtb = base_dtb; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | overlay_dtb = dtoverlay_load_dtb(overlay_file, max_dtb_size); |
| 104 | if (overlay_dtb) |
| 105 | err = dtoverlay_fixup_overlay(base_dtb, overlay_dtb); |
| 106 | else |
| 107 | err = -1; |
| 108 | } |
| 109 | |
| 110 | while (!err && (argn < argc)) |
| 111 | { |
| 112 | char *param_name = argv[argn++]; |
| 113 | char *param_value = param_name + strcspn(param_name, "=" ); |
| 114 | const void *override_data; |
| 115 | int data_len; |
| 116 | |
| 117 | if (*param_value == '=') |
| 118 | { |
| 119 | *(param_value++) = '\0'; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | /* This isn't a well-formed parameter assignment, but it can be |
| 124 | treated as an assignment of true. */ |
| 125 | param_value = "true" ; |
| 126 | } |
| 127 | |
| 128 | override_data = dtoverlay_find_override(overlay_dtb, param_name, |
| 129 | &data_len); |
| 130 | if (override_data) |
| 131 | { |
| 132 | err = dtoverlay_apply_override(overlay_dtb, param_name, |
| 133 | override_data, data_len, |
| 134 | param_value); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | override_data = dtoverlay_find_override(base_dtb, param_name, &data_len); |
| 139 | if (override_data) |
| 140 | { |
| 141 | err = dtoverlay_apply_override(base_dtb, param_name, |
| 142 | override_data, data_len, |
| 143 | param_value); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | printf("* unknown param '%s'\n" , param_name); |
| 148 | err = data_len; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (!err && (overlay_dtb != base_dtb)) |
| 154 | { |
| 155 | err = dtoverlay_merge_overlay(base_dtb, overlay_dtb); |
| 156 | |
| 157 | dtoverlay_free_dtb(overlay_dtb); |
| 158 | } |
| 159 | |
| 160 | if (!err) |
| 161 | { |
| 162 | dtoverlay_pack_dtb(base_dtb); |
| 163 | err = dtoverlay_save_dtb(base_dtb, merged_file); |
| 164 | } |
| 165 | |
| 166 | dtoverlay_free_dtb(base_dtb); |
| 167 | |
| 168 | if (err != 0) |
| 169 | printf("* Exiting with error code %d\n" , err); |
| 170 | |
| 171 | return err; |
| 172 | } |
| 173 | |