^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* These are the operations we support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) enum oper_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) OPER_WRITE_PROP, /* Write a property in a node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) OPER_CREATE_NODE, /* Create a new node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct display_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) enum oper_type oper; /* operation to perform */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int type; /* data type (s/i/u/x or 0 for default) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int size; /* data size (1/2/4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int verbose; /* verbose output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int auto_path; /* automatically create all path components */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Report an error with a particular node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @param name Node name to report error on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @param namelen Length of node name, or -1 to use entire string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @param err Error number to report (-FDT_ERR_...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void report_error(const char *name, int namelen, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (namelen == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) namelen = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) fprintf(stderr, "Error at '%1.*s': %s\n", namelen, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) fdt_strerror(err));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Encode a series of arguments in a property value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @param disp Display information / options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @param arg List of arguments from command line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @param arg_count Number of arguments (may be 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * @param valuep Returns buffer containing value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * @param *value_len Returns length of value encoded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int encode_value(struct display_info *disp, char **arg, int arg_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) char **valuep, int *value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) char *value = NULL; /* holding area for value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int value_size = 0; /* size of holding area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) char *ptr; /* pointer to current value position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int len; /* length of this cell/string/byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int ival;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int upto; /* the number of bytes we have written to buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) char fmt[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) upto = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (disp->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) fprintf(stderr, "Decoding value:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fmt[0] = '%';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) fmt[1] = disp->type ? disp->type : 'd';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) fmt[2] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) for (; arg_count > 0; arg++, arg_count--, upto += len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* assume integer unless told otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (disp->type == 's')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) len = strlen(*arg) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) len = disp->size == -1 ? 4 : disp->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* enlarge our value buffer by a suitable margin if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (upto + len > value_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) value_size = (upto + len) + 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) value = realloc(value, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) fprintf(stderr, "Out of mmory: cannot alloc "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) "%d bytes\n", value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ptr = value + upto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (disp->type == 's') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) memcpy(ptr, *arg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (disp->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) fprintf(stderr, "\tstring: '%s'\n", ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int *iptr = (int *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) sscanf(*arg, fmt, &ival);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (len == 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *iptr = cpu_to_fdt32(ival);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) *ptr = (uint8_t)ival;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (disp->verbose) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) fprintf(stderr, "\t%s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) disp->size == 1 ? "byte" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) disp->size == 2 ? "short" : "int",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ival);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *value_len = upto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) *valuep = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (disp->verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) fprintf(stderr, "Value size %d\n", upto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int store_key_value(void *blob, const char *node_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) const char *property, const char *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) node = fdt_path_offset(blob, node_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (node < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) report_error(node_name, -1, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) err = fdt_setprop(blob, node, property, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) report_error(property, -1, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * Create paths as needed for all components of a path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * Any components of the path that do not exist are created. Errors are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * reported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * @param blob FDT blob to write into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * @param in_path Path to process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * @return 0 if ok, -1 on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int create_paths(void *blob, const char *in_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) const char *path = in_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) const char *sep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int node, offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* skip leading '/' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) while (*path == '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) path++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) for (sep = path; *sep; path = sep + 1, offset = node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* equivalent to strchrnul(), but it requires _GNU_SOURCE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) sep = strchr(path, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!sep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) sep = path + strlen(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) node = fdt_subnode_offset_namelen(blob, offset, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) sep - path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (node == -FDT_ERR_NOTFOUND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) node = fdt_add_subnode_namelen(blob, offset, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) sep - path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (node < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) report_error(path, sep - path, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * Create a new node in the fdt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * This will overwrite the node_name string. Any error is reported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * TODO: Perhaps create fdt_path_offset_namelen() so we don't need to do this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * @param blob FDT blob to write into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @param node_name Name of node to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * @return new node offset if found, or -1 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static int create_node(void *blob, const char *node_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int node = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) p = strrchr(node_name, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) report_error(node_name, -1, -FDT_ERR_BADPATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (p > node_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) node = fdt_path_offset(blob, node_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (node < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) report_error(node_name, -1, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) node = fdt_add_subnode(blob, node, p + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (node < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) report_error(p + 1, -1, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static int do_fdtput(struct display_info *disp, const char *filename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) char **arg, int arg_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) char *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) char *blob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int len, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) blob = utilfdt_read(filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!blob)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) switch (disp->oper) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) case OPER_WRITE_PROP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * Convert the arguments into a single binary value, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * store them into the property.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) assert(arg_count >= 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (disp->auto_path && create_paths(blob, *arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (encode_value(disp, arg + 2, arg_count - 2, &value, &len) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) store_key_value(blob, *arg, arg[1], value, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) case OPER_CREATE_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) for (; ret >= 0 && arg_count--; arg++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (disp->auto_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ret = create_paths(blob, *arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) ret = create_node(blob, *arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) ret = utilfdt_write(filename, blob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) free(blob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static const char *usage_msg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) "fdtput - write a property value to a device tree\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) "The command line arguments are joined together into a single value.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) "Usage:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) " fdtput <options> <dt file> <node> <property> [<value>...]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) " fdtput -c <options> <dt file> [<node>...]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) "Options:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) "\t-c\t\tCreate nodes if they don't already exist\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) "\t-p\t\tAutomatically create nodes as needed for the node path\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) "\t-t <type>\tType of data\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) "\t-v\t\tVerbose: display each value decoded from command line\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) "\t-h\t\tPrint this help\n\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) USAGE_TYPE_MSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static void usage(const char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) fprintf(stderr, "Error: %s\n\n", msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) fprintf(stderr, "%s", usage_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct display_info disp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) char *filename = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) memset(&disp, '\0', sizeof(disp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) disp.size = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) disp.oper = OPER_WRITE_PROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int c = getopt(argc, argv, "chpt:v");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (c == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * TODO: add options to:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * - delete property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * - delete node (optionally recursively)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * - rename node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * - pack fdt before writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * - set amount of free space when writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * - expand fdt if value doesn't fit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) case 'c':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) disp.oper = OPER_CREATE_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) case '?':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) usage(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) disp.auto_path = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (utilfdt_decode_type(optarg, &disp.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) &disp.size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) usage("Invalid type string");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) case 'v':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) disp.verbose = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (optind < argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) filename = argv[optind++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (!filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) usage("Missing filename");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) argv += optind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) argc -= optind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (disp.oper == OPER_WRITE_PROP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (argc < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) usage("Missing node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (argc < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) usage("Missing property");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (do_fdtput(&disp, filename, argv, argc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }