1 // SPDX-License-Identifier: GPL-2.0+
3 * Building an expo from an FDT description
5 * Copyright 2022 Google LLC
9 #define LOG_CATEGORY LOGC_EXPO
15 #include <dm/ofnode.h>
16 #include <linux/libfdt.h>
19 * struct build_info - Information to use when building
21 * @str_for_id: String for each ID in use, NULL if empty. The string is NULL
22 * if there is nothing for this ID. Since ID 0 is never used, the first
23 * element of this array is always NULL
24 * @str_count: Number of entries in @str_for_id
25 * @err_node: Node being processed (for error reporting)
26 * @err_prop: Property being processed (for error reporting)
29 const char **str_for_id;
36 * add_txt_str - Add a string or lookup its ID, then add to expo
38 * @info: Build information
39 * @node: Node describing scene
40 * @scn: Scene to add to
41 * @find_name: Name to look for (e.g. "title"). This will find a property called
42 * "title" if it exists, else will look up the string for "title-id"
43 * Return: ID of added string, or -ve on error
45 int add_txt_str(struct build_info *info, ofnode node, struct scene *scn,
46 const char *find_name, uint obj_id)
52 info->err_prop = find_name;
53 text = ofnode_read_string(node, find_name);
58 snprintf(name, sizeof(name), "%s-id", find_name);
59 ret = ofnode_read_u32(node, name, &id);
61 return log_msg_ret("id", -ENOENT);
63 if (id >= info->str_count)
64 return log_msg_ret("id", -E2BIG);
65 text = info->str_for_id[id];
67 return log_msg_ret("id", -EINVAL);
70 ret = expo_str(scn->expo, find_name, 0, text);
72 return log_msg_ret("add", ret);
75 ret = scene_txt_str(scn, find_name, obj_id, str_id, text, NULL);
77 return log_msg_ret("add", ret);
83 * add_txt_str_list - Add a list string or lookup its ID, then add to expo
85 * @info: Build information
86 * @node: Node describing scene
87 * @scn: Scene to add to
88 * @find_name: Name to look for (e.g. "title"). This will find a string-list
89 * property called "title" if it exists, else will look up the string in the
90 * "title-id" string list.
91 * Return: ID of added string, or -ve on error
93 int add_txt_str_list(struct build_info *info, ofnode node, struct scene *scn,
94 const char *find_name, int index, uint obj_id)
100 ret = ofnode_read_string_index(node, find_name, index, &text);
105 snprintf(name, sizeof(name), "%s-id", find_name);
106 ret = ofnode_read_u32_index(node, name, index, &id);
108 return log_msg_ret("id", -ENOENT);
110 if (id >= info->str_count)
111 return log_msg_ret("id", -E2BIG);
112 text = info->str_for_id[id];
114 return log_msg_ret("id", -EINVAL);
117 ret = expo_str(scn->expo, find_name, 0, text);
119 return log_msg_ret("add", ret);
122 ret = scene_txt_str(scn, find_name, obj_id, str_id, text, NULL);
124 return log_msg_ret("add", ret);
130 * build_element() - Handle creating a text object from a label
132 * Look up a property called @label or @label-id and create a string for it
134 int build_element(void *ldtb, int node, const char *label)
140 * read_strings() - Read in the list of strings
142 * Read the strings into an ID-indexed list, so they can be used for building
143 * an expo. The strings are in a /strings node and each has its own subnode
144 * containing the ID and the string itself:
148 * value = "This is a test";
151 * Future work may add support for unicode and multiple languages
153 * @info: Build information
154 * @root: Root node to read from
155 * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
158 static int read_strings(struct build_info *info, ofnode root)
160 ofnode strings, node;
162 strings = ofnode_find_subnode(root, "strings");
163 if (!ofnode_valid(strings))
164 return log_msg_ret("str", -EINVAL);
166 ofnode_for_each_subnode(node, strings) {
171 info->err_node = node;
172 ret = ofnode_read_u32(node, "id", &id);
174 return log_msg_ret("id", -ENOENT);
175 val = ofnode_read_string(node, "value");
177 return log_msg_ret("val", -EINVAL);
179 if (id >= info->str_count) {
180 int new_count = info->str_count + 20;
183 new_arr = realloc(info->str_for_id,
184 new_count * sizeof(char *));
186 return log_msg_ret("id", -ENOMEM);
187 memset(new_arr + info->str_count, '\0',
188 (new_count - info->str_count) * sizeof(char *));
189 info->str_for_id = new_arr;
190 info->str_count = new_count;
193 info->str_for_id[id] = val;
200 * list_strings() - List the available strings with their IDs
202 * @info: Build information
204 static void list_strings(struct build_info *info)
208 for (i = 0; i < info->str_count; i++) {
209 if (info->str_for_id[i])
210 printf("%3d %s\n", i, info->str_for_id[i]);
215 * menu_build() - Build a menu and add it to a scene
217 * See doc/develop/expo.rst for a description of the format
219 * @info: Build information
220 * @node: Node containing the menu description
221 * @scn: Scene to add the menu to
222 * @id: ID for the menu
223 * @objp: Returns the object pointer
224 * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
225 * error, -ENOENT if there is a references to a non-existent string
227 static int menu_build(struct build_info *info, ofnode node, struct scene *scn,
228 uint id, struct scene_obj **objp)
230 struct scene_obj_menu *menu;
231 uint title_id, menu_id;
236 name = ofnode_get_name(node);
238 ret = scene_menu(scn, name, id, &menu);
240 return log_msg_ret("men", ret);
244 ret = add_txt_str(info, node, scn, "title", 0);
246 return log_msg_ret("tit", ret);
248 ret = scene_menu_set_title(scn, menu_id, title_id);
250 return log_msg_ret("set", ret);
252 item_ids = ofnode_read_prop(node, "item-id", &size);
254 return log_msg_ret("itm", -EINVAL);
255 if (!size || size % sizeof(u32))
256 return log_msg_ret("isz", -EINVAL);
259 for (i = 0; i < size; i++) {
260 struct scene_menitem *item;
261 uint label, key, desc;
263 ret = add_txt_str_list(info, node, scn, "item-label", i, 0);
264 if (ret < 0 && ret != -ENOENT)
265 return log_msg_ret("lab", ret);
268 ret = add_txt_str_list(info, node, scn, "key-label", i, 0);
269 if (ret < 0 && ret != -ENOENT)
270 return log_msg_ret("key", ret);
273 ret = add_txt_str_list(info, node, scn, "desc-label", i, 0);
274 if (ret < 0 && ret != -ENOENT)
275 return log_msg_ret("lab", ret);
278 ret = scene_menuitem(scn, menu_id, simple_xtoa(i),
279 fdt32_to_cpu(item_ids[i]), key, label,
282 return log_msg_ret("mi", ret);
289 static int textline_build(struct build_info *info, ofnode node,
290 struct scene *scn, uint id, struct scene_obj **objp)
292 struct scene_obj_textline *ted;
293 uint ted_id, edit_id;
298 name = ofnode_get_name(node);
300 info->err_prop = "max-chars";
301 ret = ofnode_read_u32(node, "max-chars", &max_chars);
303 return log_msg_ret("max", -ENOENT);
305 ret = scene_textline(scn, name, id, max_chars, &ted);
307 return log_msg_ret("ted", ret);
311 ret = add_txt_str(info, node, scn, "title", 0);
313 return log_msg_ret("tit", ret);
316 /* Setup the editor */
317 info->err_prop = "edit-id";
318 ret = ofnode_read_u32(node, "edit-id", &id);
320 return log_msg_ret("id", -ENOENT);
323 ret = scene_txt_str(scn, "edit", edit_id, 0, abuf_data(&ted->buf),
326 return log_msg_ret("add", ret);
333 * obj_build() - Build an expo object and add it to a scene
335 * See doc/develop/expo.rst for a description of the format
337 * @info: Build information
338 * @node: Node containing the object description
339 * @scn: Scene to add the object to
340 * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
341 * error, -ENOENT if there is a references to a non-existent string
343 static int obj_build(struct build_info *info, ofnode node, struct scene *scn)
345 struct scene_obj *obj;
350 log_debug("- object %s\n", ofnode_get_name(node));
351 ret = ofnode_read_u32(node, "id", &id);
353 return log_msg_ret("id", -ENOENT);
355 type = ofnode_read_string(node, "type");
357 return log_msg_ret("typ", -EINVAL);
359 if (!strcmp("menu", type))
360 ret = menu_build(info, node, scn, id, &obj);
361 else if (!strcmp("textline", type))
362 ret = textline_build(info, node, scn, id, &obj);
366 return log_msg_ret("bld", ret);
368 if (!ofnode_read_u32(node, "start-bit", &val))
369 obj->start_bit = val;
370 if (!ofnode_read_u32(node, "bit-length", &val))
371 obj->bit_length = val;
377 * scene_build() - Build a scene and all its objects
379 * See doc/develop/expo.rst for a description of the format
381 * @info: Build information
382 * @node: Node containing the scene description
383 * @scn: Scene to add the object to
384 * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
385 * error, -ENOENT if there is a references to a non-existent string
387 static int scene_build(struct build_info *info, ofnode scn_node,
396 info->err_node = scn_node;
397 name = ofnode_get_name(scn_node);
398 log_debug("Building scene %s\n", name);
399 ret = ofnode_read_u32(scn_node, "id", &id);
401 return log_msg_ret("id", -ENOENT);
403 ret = scene_new(exp, name, id, &scn);
405 return log_msg_ret("scn", ret);
407 ret = add_txt_str(info, scn_node, scn, "title", 0);
409 return log_msg_ret("tit", ret);
411 scene_title_set(scn, title_id);
413 ret = add_txt_str(info, scn_node, scn, "prompt", 0);
415 return log_msg_ret("pr", ret);
417 ofnode_for_each_subnode(node, scn_node) {
418 info->err_node = node;
419 ret = obj_build(info, node, scn);
421 return log_msg_ret("mit", ret);
427 int build_it(struct build_info *info, ofnode root, struct expo **expp)
434 ret = read_strings(info, root);
436 return log_msg_ret("str", ret);
439 info->err_node = root;
441 ret = expo_new("name", NULL, &exp);
443 return log_msg_ret("exp", ret);
445 if (!ofnode_read_u32(root, "dynamic-start", &dyn_start))
446 expo_set_dynamic_start(exp, dyn_start);
448 scenes = ofnode_find_subnode(root, "scenes");
449 if (!ofnode_valid(scenes))
450 return log_msg_ret("sno", -EINVAL);
452 ofnode_for_each_subnode(node, scenes) {
453 ret = scene_build(info, node, exp);
455 return log_msg_ret("scn", ret);
462 int expo_build(ofnode root, struct expo **expp)
464 struct build_info info;
468 memset(&info, '\0', sizeof(info));
469 ret = build_it(&info, root, &exp);
474 node_ret = ofnode_get_path(info.err_node, buf, sizeof(buf));
475 log_warning("Build failed at node %s, property %s\n",
476 node_ret ? ofnode_get_name(info.err_node) : buf,
479 return log_msg_ret("bui", ret);