summaryrefslogtreecommitdiff
path: root/tests/src/ufdt_gen_test_dts.c
blob: 69a4c3c5bbeaea1b491541122a2a19daec31e31e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/*
 * The parameters to generate testing DTS
 * /dts-v1/ /plugin/;           <- header and plugin
 * /{
 *   level0 {                   <- depth
 *     level1 {
 *       ...
 *       node0: node0 {         <- node
 *         unused0 {}           <- unused
 *         unused1 {}
 *         ...
 *         status="disabled";
 *       }
 *       ...
 *     };
 *   };
 * };
 *
 * &node0 {                     <- append
 *    new_prop="foo";
 * }
 * ...
 *
 * &node0 {                     <- override
 *    status="okay";
 * }
 * ...
 */

static const char short_options[] = "Hpd:u:n:a:w:o:";
static struct option long_options[] = {
  { "no-header",    no_argument,       NULL, 'H' },
  { "plugin",       no_argument,       NULL, 'p' },
  { "depth",        required_argument, NULL, 'd' },
  { "unused",       required_argument, NULL, 'u' },
  { "node",         required_argument, NULL, 'n' },
  { "append",       required_argument, NULL, 'a' },
  { "override",     required_argument, NULL, 'w' },
  { "output",       required_argument, NULL, 'o' },
  { 0,              0,                 NULL, 0 }
};

struct gen_params {
  int no_header;    /* Doesn't add header */
  int plugin;       /* Add /plugin/ in header */
  int depth;        /* the depth of a node, 0 means generate on root node */
  int unused_num;   /* unused child nodes per node */
  int node_num;     /* the number to generate nodes */
  int append_num;   /* the number to generate appending references */
  int override_num; /* the number to generate overriding references */
};


static void output_header(FILE *fp, int is_plugin) {
  fprintf(fp, "/dts-v1/;\n");
  if (is_plugin) {
    fprintf(fp, "/plugin/;\n");
  }
  fprintf(fp, "\n");
}

static void output_root_begin(FILE *fp, int depth) {
  fprintf(fp, "/ {\n");

  int i;
  for (i = 0; i < depth; i++) {
    fprintf(fp, "level%d {\n", i);
  }
}

static void output_root_end(FILE *fp, int depth) {
  int i;
  for (i = 0; i < depth; i++) {
    fprintf(fp, "};\n");
  }

  fprintf(fp, "};\n\n");
}

static void output_unused_nodes(FILE *fp, int count) {
  int i;
  for (i = 0; i < count; i++) {
    fprintf(fp, "unused%d {};\n", i);
  }
}

static void output_prop_str(FILE *fp, const char *prop, const char *value) {
  /* TODO: should escape value */
  fprintf(fp, "%s=\"%s\";\n", prop, value);
}

static void output_nodes(FILE *fp, int count, const char *prop, const char *value) {
  int i;
  for (i = 0; i < count; i++) {
    fprintf(fp, "node%d: node%d {\n", i, i);
    output_prop_str(fp, prop, value);
    fprintf(fp, "};\n\n");
  }
}

static void output_ref_nodes(FILE *fp, int start_id, int count,
                      const char *prop, const char *value) {
  int i;
  for (i = start_id; i < start_id + count; i++) {
    fprintf(fp, "&node%d {\n", i);
    output_prop_str(fp, prop, value);
    fprintf(fp, "};\n\n");
  }
}

static int gen_dts(FILE *fp, const struct gen_params *params) {
  if (!params->no_header) {
    output_header(fp, params->plugin);
  }

  if (params->node_num > 0) {
    output_root_begin(fp, params->depth);
    output_unused_nodes(fp, params->unused_num);
    output_nodes(fp, params->node_num, "status", "disabled");
    output_root_end(fp, params->depth);
  }

  int start_id = 0;
  output_ref_nodes(fp, start_id, params->append_num, "new_prop", "bar");
  start_id += params->append_num;
  output_ref_nodes(fp, start_id, params->override_num, "status", "okay");

  return 0;
}

int main(int argc, char *argv[]) {
  const char *filename = NULL;
  struct gen_params params;
  memset(&params, 0, sizeof(struct gen_params));

  while (1) {
    int option_index = 0;
    int c = getopt_long(argc, argv, short_options, long_options, &option_index);
    if (c == -1) {
      break;
    }
    switch (c) {
    case 'H':
      params.no_header = 1;
      break;
    case 'p':
      params.plugin = 1;
      break;
    case 'd':
      params.depth = atoi(optarg);
      break;
    case 'u':
      params.unused_num = atoi(optarg);
      break;
    case 'n':
      params.node_num = atoi(optarg);
      break;
    case 'a':
      params.append_num = atoi(optarg);
      break;
    case 'w':
      params.override_num = atoi(optarg);
      break;
    case 'o':
      filename = optarg;
      break;
    case '?':
      break;
    }
  }

  FILE *fp = NULL;
  if (filename) {
    fp = fopen(filename, "wt");
    if (fp == NULL) {
      fprintf(stderr, "Can not create file: ", filename);
      return -1;
    }
  }

  gen_dts(fp ? fp : stdout, &params);

  if (fp) {
    fclose(fp);
  }

  return 0;
}