expo_build.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Building an expo from an FDT description
  4. *
  5. * Copyright 2022 Google LLC
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. #define LOG_CATEGORY LOGC_EXPO
  9. #include <common.h>
  10. #include <expo.h>
  11. #include <fdtdec.h>
  12. #include <log.h>
  13. #include <malloc.h>
  14. #include <dm/ofnode.h>
  15. #include <linux/libfdt.h>
  16. /**
  17. * struct build_info - Information to use when building
  18. *
  19. * @str_for_id: String for each ID in use, NULL if empty. The string is NULL
  20. * if there is nothing for this ID. Since ID 0 is never used, the first
  21. * element of this array is always NULL
  22. * @str_count: Number of entries in @str_for_id
  23. */
  24. struct build_info {
  25. const char **str_for_id;
  26. int str_count;
  27. };
  28. /**
  29. * add_txt_str - Add a string or lookup its ID, then add to expo
  30. *
  31. * @info: Build information
  32. * @node: Node describing scene
  33. * @scn: Scene to add to
  34. * @find_name: Name to look for (e.g. "title"). This will find a property called
  35. * "title" if it exists, else will look up the string for "title-id"
  36. * Return: ID of added string, or -ve on error
  37. */
  38. int add_txt_str(struct build_info *info, ofnode node, struct scene *scn,
  39. const char *find_name, uint obj_id)
  40. {
  41. const char *text;
  42. uint str_id;
  43. int ret;
  44. text = ofnode_read_string(node, find_name);
  45. if (!text) {
  46. char name[40];
  47. u32 id;
  48. snprintf(name, sizeof(name), "%s-id", find_name);
  49. ret = ofnode_read_u32(node, name, &id);
  50. if (ret)
  51. return log_msg_ret("id", -EINVAL);
  52. if (id >= info->str_count)
  53. return log_msg_ret("id", -E2BIG);
  54. text = info->str_for_id[id];
  55. if (!text)
  56. return log_msg_ret("id", -EINVAL);
  57. }
  58. ret = expo_str(scn->expo, find_name, 0, text);
  59. if (ret < 0)
  60. return log_msg_ret("add", ret);
  61. str_id = ret;
  62. ret = scene_txt_str(scn, find_name, obj_id, str_id, text, NULL);
  63. if (ret < 0)
  64. return log_msg_ret("add", ret);
  65. return ret;
  66. }
  67. /**
  68. * add_txt_str_list - Add a list string or lookup its ID, then add to expo
  69. *
  70. * @info: Build information
  71. * @node: Node describing scene
  72. * @scn: Scene to add to
  73. * @find_name: Name to look for (e.g. "title"). This will find a string-list
  74. * property called "title" if it exists, else will look up the string in the
  75. * "title-id" string list.
  76. * Return: ID of added string, or -ve on error
  77. */
  78. int add_txt_str_list(struct build_info *info, ofnode node, struct scene *scn,
  79. const char *find_name, int index, uint obj_id)
  80. {
  81. const char *text;
  82. uint str_id;
  83. int ret;
  84. ret = ofnode_read_string_index(node, find_name, index, &text);
  85. if (ret) {
  86. char name[40];
  87. u32 id;
  88. snprintf(name, sizeof(name), "%s-id", find_name);
  89. ret = ofnode_read_u32_index(node, name, index, &id);
  90. if (ret)
  91. return log_msg_ret("id", -ENOENT);
  92. if (id >= info->str_count)
  93. return log_msg_ret("id", -E2BIG);
  94. text = info->str_for_id[id];
  95. if (!text)
  96. return log_msg_ret("id", -EINVAL);
  97. }
  98. ret = expo_str(scn->expo, find_name, 0, text);
  99. if (ret < 0)
  100. return log_msg_ret("add", ret);
  101. str_id = ret;
  102. ret = scene_txt_str(scn, find_name, obj_id, str_id, text, NULL);
  103. if (ret < 0)
  104. return log_msg_ret("add", ret);
  105. return ret;
  106. }
  107. /*
  108. * build_element() - Handle creating a text object from a label
  109. *
  110. * Look up a property called @label or @label-id and create a string for it
  111. */
  112. int build_element(void *ldtb, int node, const char *label)
  113. {
  114. return 0;
  115. }
  116. /**
  117. * read_strings() - Read in the list of strings
  118. *
  119. * Read the strings into an ID-indexed list, so they can be used for building
  120. * an expo. The strings are in a /strings node and each has its own subnode
  121. * containing the ID and the string itself:
  122. *
  123. * example {
  124. * id = <123>;
  125. * value = "This is a test";
  126. * };
  127. *
  128. * Future work may add support for unicode and multiple languages
  129. *
  130. * @info: Build information
  131. * @root: Root node to read from
  132. * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
  133. * error
  134. */
  135. static int read_strings(struct build_info *info, ofnode root)
  136. {
  137. ofnode strings, node;
  138. strings = ofnode_find_subnode(root, "strings");
  139. if (!ofnode_valid(strings))
  140. return log_msg_ret("str", -EINVAL);
  141. ofnode_for_each_subnode(node, strings) {
  142. const char *val;
  143. int ret;
  144. u32 id;
  145. ret = ofnode_read_u32(node, "id", &id);
  146. if (ret)
  147. return log_msg_ret("id", -EINVAL);
  148. val = ofnode_read_string(node, "value");
  149. if (!val)
  150. return log_msg_ret("val", -EINVAL);
  151. if (id >= info->str_count) {
  152. int new_count = info->str_count + 20;
  153. void *new_arr;
  154. new_arr = realloc(info->str_for_id,
  155. new_count * sizeof(char *));
  156. if (!new_arr)
  157. return log_msg_ret("id", -ENOMEM);
  158. memset(new_arr + info->str_count, '\0',
  159. (new_count - info->str_count) * sizeof(char *));
  160. info->str_for_id = new_arr;
  161. info->str_count = new_count;
  162. }
  163. info->str_for_id[id] = val;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * list_strings() - List the available strings with their IDs
  169. *
  170. * @info: Build information
  171. */
  172. static void list_strings(struct build_info *info)
  173. {
  174. int i;
  175. for (i = 0; i < info->str_count; i++) {
  176. if (info->str_for_id[i])
  177. printf("%3d %s\n", i, info->str_for_id[i]);
  178. }
  179. }
  180. /**
  181. * menu_build() - Build a menu and add it to a scene
  182. *
  183. * See doc/develop/expo.rst for a description of the format
  184. *
  185. * @info: Build information
  186. * @node: Node containing the menu description
  187. * @scn: Scene to add the menu to
  188. * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
  189. * error, -ENOENT if there is a references to a non-existent string
  190. */
  191. static int menu_build(struct build_info *info, ofnode node, struct scene *scn)
  192. {
  193. struct scene_obj_menu *menu;
  194. uint title_id, menu_id;
  195. const u32 *item_ids;
  196. int ret, size, i;
  197. const char *name;
  198. u32 id;
  199. name = ofnode_get_name(node);
  200. ret = ofnode_read_u32(node, "id", &id);
  201. if (ret)
  202. return log_msg_ret("id", -EINVAL);
  203. ret = scene_menu(scn, name, id, &menu);
  204. if (ret < 0)
  205. return log_msg_ret("men", ret);
  206. menu_id = ret;
  207. /* Set the title */
  208. ret = add_txt_str(info, node, scn, "title", 0);
  209. if (ret < 0)
  210. return log_msg_ret("tit", ret);
  211. title_id = ret;
  212. ret = scene_menu_set_title(scn, menu_id, title_id);
  213. item_ids = ofnode_read_prop(node, "item-id", &size);
  214. if (!item_ids)
  215. return log_msg_ret("itm", -EINVAL);
  216. if (!size || size % sizeof(u32))
  217. return log_msg_ret("isz", -EINVAL);
  218. size /= sizeof(u32);
  219. for (i = 0; i < size; i++) {
  220. struct scene_menitem *item;
  221. uint label, key, desc;
  222. ret = add_txt_str_list(info, node, scn, "item-label", i, 0);
  223. if (ret < 0 && ret != -ENOENT)
  224. return log_msg_ret("lab", ret);
  225. label = max(0, ret);
  226. ret = add_txt_str_list(info, node, scn, "key-label", i, 0);
  227. if (ret < 0 && ret != -ENOENT)
  228. return log_msg_ret("key", ret);
  229. key = max(0, ret);
  230. ret = add_txt_str_list(info, node, scn, "desc-label", i, 0);
  231. if (ret < 0 && ret != -ENOENT)
  232. return log_msg_ret("lab", ret);
  233. desc = max(0, ret);
  234. ret = scene_menuitem(scn, menu_id, simple_xtoa(i),
  235. fdt32_to_cpu(item_ids[i]), key, label,
  236. desc, 0, 0, &item);
  237. if (ret < 0)
  238. return log_msg_ret("mi", ret);
  239. }
  240. return 0;
  241. }
  242. /**
  243. * menu_build() - Build an expo object and add it to a scene
  244. *
  245. * See doc/develop/expo.rst for a description of the format
  246. *
  247. * @info: Build information
  248. * @node: Node containing the object description
  249. * @scn: Scene to add the object to
  250. * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
  251. * error, -ENOENT if there is a references to a non-existent string
  252. */
  253. static int obj_build(struct build_info *info, ofnode node, struct scene *scn)
  254. {
  255. const char *type;
  256. u32 id;
  257. int ret;
  258. log_debug("- object %s\n", ofnode_get_name(node));
  259. ret = ofnode_read_u32(node, "id", &id);
  260. if (ret)
  261. return log_msg_ret("id", -EINVAL);
  262. type = ofnode_read_string(node, "type");
  263. if (!type)
  264. return log_msg_ret("typ", -EINVAL);
  265. if (!strcmp("menu", type))
  266. ret = menu_build(info, node, scn);
  267. else
  268. ret = -EINVAL;
  269. if (ret)
  270. return log_msg_ret("bld", ret);
  271. return 0;
  272. }
  273. /**
  274. * scene_build() - Build a scene and all its objects
  275. *
  276. * See doc/develop/expo.rst for a description of the format
  277. *
  278. * @info: Build information
  279. * @node: Node containing the scene description
  280. * @scn: Scene to add the object to
  281. * Returns: 0 if OK, -ENOMEM if out of memory, -EINVAL if there is a format
  282. * error, -ENOENT if there is a references to a non-existent string
  283. */
  284. static int scene_build(struct build_info *info, ofnode scn_node,
  285. struct expo *exp)
  286. {
  287. const char *name;
  288. struct scene *scn;
  289. uint id, title_id;
  290. ofnode node;
  291. int ret;
  292. name = ofnode_get_name(scn_node);
  293. log_debug("Building scene %s\n", name);
  294. ret = ofnode_read_u32(scn_node, "id", &id);
  295. if (ret)
  296. return log_msg_ret("id", -EINVAL);
  297. ret = scene_new(exp, name, id, &scn);
  298. if (ret < 0)
  299. return log_msg_ret("scn", ret);
  300. ret = add_txt_str(info, scn_node, scn, "title", 0);
  301. if (ret < 0)
  302. return log_msg_ret("tit", ret);
  303. title_id = ret;
  304. scene_title_set(scn, title_id);
  305. ret = add_txt_str(info, scn_node, scn, "prompt", 0);
  306. if (ret < 0)
  307. return log_msg_ret("pr", ret);
  308. ofnode_for_each_subnode(node, scn_node) {
  309. ret = obj_build(info, node, scn);
  310. if (ret < 0)
  311. return log_msg_ret("mit", ret);
  312. }
  313. return 0;
  314. }
  315. int expo_build(ofnode root, struct expo **expp)
  316. {
  317. struct build_info info;
  318. ofnode scenes, node;
  319. struct expo *exp;
  320. u32 dyn_start;
  321. int ret;
  322. memset(&info, '\0', sizeof(info));
  323. ret = read_strings(&info, root);
  324. if (ret)
  325. return log_msg_ret("str", ret);
  326. if (_DEBUG)
  327. list_strings(&info);
  328. ret = expo_new("name", NULL, &exp);
  329. if (ret)
  330. return log_msg_ret("exp", ret);
  331. if (!ofnode_read_u32(root, "dynamic-start", &dyn_start))
  332. expo_set_dynamic_start(exp, dyn_start);
  333. scenes = ofnode_find_subnode(root, "scenes");
  334. if (!ofnode_valid(scenes))
  335. return log_msg_ret("sno", -EINVAL);
  336. ofnode_for_each_subnode(node, scenes) {
  337. ret = scene_build(&info, node, exp);
  338. if (ret < 0)
  339. return log_msg_ret("scn", ret);
  340. }
  341. *expp = exp;
  342. return 0;
  343. }