fdt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/fdt.h>
  19. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/dl.h>
  22. GRUB_MOD_LICENSE ("GPLv3+");
  23. #define FDT_SUPPORTED_VERSION 17
  24. #define FDT_BEGIN_NODE 0x00000001
  25. #define FDT_END_NODE 0x00000002
  26. #define FDT_PROP 0x00000003
  27. #define FDT_NOP 0x00000004
  28. #define FDT_END 0x00000009
  29. #define struct_end(fdt) \
  30. ((grub_addr_t) fdt + grub_fdt_get_off_dt_struct(fdt) \
  31. + grub_fdt_get_size_dt_struct(fdt))
  32. /* Size needed by a node entry: 2 tokens (FDT_BEGIN_NODE and FDT_END_NODE), plus
  33. the NULL-terminated string containing the name, plus padding if needed. */
  34. #define node_entry_size(node_name) \
  35. (2 * sizeof(grub_uint32_t) \
  36. + ALIGN_UP (grub_strlen (name) + 1, sizeof(grub_uint32_t)))
  37. #define SKIP_NODE_NAME(name, token, end) \
  38. name = (char *) ((token) + 1); \
  39. while (name < (char *) end) \
  40. { \
  41. if (!*name++) \
  42. break; \
  43. } \
  44. token = (grub_uint32_t *) ALIGN_UP((grub_addr_t) (name), sizeof(*token))
  45. static grub_uint32_t *get_next_node (const void *fdt, char *node_name)
  46. {
  47. grub_uint32_t *end = (void *) struct_end (fdt);
  48. grub_uint32_t *token;
  49. if (node_name >= (char *) end)
  50. return NULL;
  51. while (*node_name++)
  52. {
  53. if (node_name >= (char *) end)
  54. return NULL;
  55. }
  56. token = (grub_uint32_t *) ALIGN_UP ((grub_addr_t) node_name, 4);
  57. while (token < end)
  58. {
  59. switch (grub_be_to_cpu32(*token))
  60. {
  61. case FDT_BEGIN_NODE:
  62. token = get_next_node (fdt, (char *) (token + 1));
  63. if (!token)
  64. return NULL;
  65. break;
  66. case FDT_END_NODE:
  67. token++;
  68. if (token >= end)
  69. return NULL;
  70. return token;
  71. case FDT_PROP:
  72. /* Skip property token and following data (len, nameoff and property
  73. value). */
  74. token += grub_fdt_prop_entry_size(grub_be_to_cpu32(*(token + 1)))
  75. / sizeof(*token);
  76. break;
  77. case FDT_NOP:
  78. token++;
  79. break;
  80. default:
  81. return NULL;
  82. }
  83. }
  84. return NULL;
  85. }
  86. static int get_mem_rsvmap_size (const void *fdt)
  87. {
  88. int size = 0;
  89. grub_unaligned_uint64_t *ptr = (void *) ((grub_addr_t) fdt
  90. + grub_fdt_get_off_mem_rsvmap (fdt));
  91. do
  92. {
  93. size += 2 * sizeof(*ptr);
  94. if (!ptr[0].val && !ptr[1].val)
  95. return size;
  96. ptr += 2;
  97. } while ((grub_addr_t) ptr <= (grub_addr_t) fdt + grub_fdt_get_totalsize (fdt)
  98. - 2 * sizeof(grub_uint64_t));
  99. return -1;
  100. }
  101. static grub_uint32_t get_free_space (void *fdt)
  102. {
  103. int mem_rsvmap_size = get_mem_rsvmap_size (fdt);
  104. if (mem_rsvmap_size < 0)
  105. /* invalid memory reservation block */
  106. return 0;
  107. return (grub_fdt_get_totalsize (fdt) - sizeof(grub_fdt_header_t)
  108. - mem_rsvmap_size - grub_fdt_get_size_dt_strings (fdt)
  109. - grub_fdt_get_size_dt_struct (fdt));
  110. }
  111. static int add_subnode (void *fdt, int parentoffset, const char *name)
  112. {
  113. grub_uint32_t *token = (void *) ((grub_addr_t) fdt
  114. + grub_fdt_get_off_dt_struct(fdt)
  115. + parentoffset);
  116. grub_uint32_t *end = (void *) struct_end (fdt);
  117. unsigned int entry_size = node_entry_size (name);
  118. unsigned int struct_size = grub_fdt_get_size_dt_struct(fdt);
  119. char *node_name;
  120. SKIP_NODE_NAME(node_name, token, end);
  121. /* Insert the new subnode just after the properties of the parent node (if
  122. any).*/
  123. while (1)
  124. {
  125. if (token >= end)
  126. return -1;
  127. switch (grub_be_to_cpu32(*token))
  128. {
  129. case FDT_PROP:
  130. /* Skip len, nameoff and property value. */
  131. token += grub_fdt_prop_entry_size(grub_be_to_cpu32(*(token + 1)))
  132. / sizeof(*token);
  133. break;
  134. case FDT_BEGIN_NODE:
  135. case FDT_END_NODE:
  136. goto insert;
  137. case FDT_NOP:
  138. token++;
  139. break;
  140. default:
  141. /* invalid token */
  142. return -1;
  143. }
  144. }
  145. insert:
  146. grub_memmove (token + entry_size / sizeof(*token), token,
  147. (grub_addr_t) end - (grub_addr_t) token);
  148. *token = grub_cpu_to_be32_compile_time(FDT_BEGIN_NODE);
  149. token[entry_size / sizeof(*token) - 2] = 0; /* padding bytes */
  150. grub_strcpy((char *) (token + 1), name);
  151. token[entry_size / sizeof(*token) - 1] = grub_cpu_to_be32_compile_time(FDT_END_NODE);
  152. grub_fdt_set_size_dt_struct (fdt, struct_size + entry_size);
  153. return ((grub_addr_t) token - (grub_addr_t) fdt
  154. - grub_fdt_get_off_dt_struct(fdt));
  155. }
  156. /* Rearrange FDT blocks in the canonical order: first the memory reservation
  157. block (just after the FDT header), then the structure block and finally the
  158. strings block. No free space is left between the first and the second block,
  159. while the space between the second and the third block is given by the
  160. clearance argument. */
  161. static int rearrange_blocks (void *fdt, unsigned int clearance)
  162. {
  163. grub_uint32_t off_mem_rsvmap = ALIGN_UP(sizeof(grub_fdt_header_t), 8);
  164. grub_uint32_t off_dt_struct = off_mem_rsvmap + get_mem_rsvmap_size (fdt);
  165. grub_uint32_t off_dt_strings = off_dt_struct
  166. + grub_fdt_get_size_dt_struct (fdt)
  167. + clearance;
  168. grub_uint8_t *fdt_ptr = fdt;
  169. grub_uint8_t *tmp_fdt;
  170. if ((grub_fdt_get_off_mem_rsvmap (fdt) == off_mem_rsvmap)
  171. && (grub_fdt_get_off_dt_struct (fdt) == off_dt_struct))
  172. {
  173. /* No need to allocate memory for a temporary FDT, just move the strings
  174. block if needed. */
  175. if (grub_fdt_get_off_dt_strings (fdt) != off_dt_strings)
  176. {
  177. grub_memmove(fdt_ptr + off_dt_strings,
  178. fdt_ptr + grub_fdt_get_off_dt_strings (fdt),
  179. grub_fdt_get_size_dt_strings (fdt));
  180. grub_fdt_set_off_dt_strings (fdt, off_dt_strings);
  181. }
  182. return 0;
  183. }
  184. tmp_fdt = grub_malloc (grub_fdt_get_totalsize (fdt));
  185. if (!tmp_fdt)
  186. return -1;
  187. grub_memcpy (tmp_fdt + off_mem_rsvmap,
  188. fdt_ptr + grub_fdt_get_off_mem_rsvmap (fdt),
  189. get_mem_rsvmap_size (fdt));
  190. grub_fdt_set_off_mem_rsvmap (fdt, off_mem_rsvmap);
  191. grub_memcpy (tmp_fdt + off_dt_struct,
  192. fdt_ptr + grub_fdt_get_off_dt_struct (fdt),
  193. grub_fdt_get_size_dt_struct (fdt));
  194. grub_fdt_set_off_dt_struct (fdt, off_dt_struct);
  195. grub_memcpy (tmp_fdt + off_dt_strings,
  196. fdt_ptr + grub_fdt_get_off_dt_strings (fdt),
  197. grub_fdt_get_size_dt_strings (fdt));
  198. grub_fdt_set_off_dt_strings (fdt, off_dt_strings);
  199. /* Copy reordered blocks back to fdt. */
  200. grub_memcpy (fdt_ptr + off_mem_rsvmap, tmp_fdt + off_mem_rsvmap,
  201. grub_fdt_get_totalsize (fdt) - off_mem_rsvmap);
  202. grub_free(tmp_fdt);
  203. return 0;
  204. }
  205. static grub_uint32_t *find_prop (const void *fdt, unsigned int nodeoffset,
  206. const char *name)
  207. {
  208. grub_uint32_t *prop = (void *) ((grub_addr_t) fdt
  209. + grub_fdt_get_off_dt_struct (fdt)
  210. + nodeoffset);
  211. grub_uint32_t *end = (void *) struct_end(fdt);
  212. grub_uint32_t nameoff;
  213. char *node_name;
  214. SKIP_NODE_NAME(node_name, prop, end);
  215. while (prop < end - 2)
  216. {
  217. if (grub_be_to_cpu32(*prop) == FDT_PROP)
  218. {
  219. nameoff = grub_be_to_cpu32(*(prop + 2));
  220. if ((nameoff + grub_strlen (name) < grub_fdt_get_size_dt_strings (fdt))
  221. && !grub_strcmp (name, (char *) fdt +
  222. grub_fdt_get_off_dt_strings (fdt) + nameoff))
  223. {
  224. if (prop + grub_fdt_prop_entry_size(grub_be_to_cpu32(*(prop + 1)))
  225. / sizeof (*prop) >= end)
  226. return NULL;
  227. return prop;
  228. }
  229. prop += grub_fdt_prop_entry_size(grub_be_to_cpu32(*(prop + 1))) / sizeof (*prop);
  230. }
  231. else if (grub_be_to_cpu32(*prop) == FDT_NOP)
  232. prop++;
  233. else
  234. return NULL;
  235. }
  236. return NULL;
  237. }
  238. /* Check the FDT header for consistency and adjust the totalsize field to match
  239. the size allocated for the FDT; if this function is called before the other
  240. functions in this file and returns success, the other functions are
  241. guaranteed not to access memory locations outside the allocated memory. */
  242. int grub_fdt_check_header_nosize (const void *fdt)
  243. {
  244. if (((grub_addr_t) fdt & 0x3) || (grub_fdt_get_magic (fdt) != FDT_MAGIC)
  245. || (grub_fdt_get_version (fdt) < FDT_SUPPORTED_VERSION)
  246. || (grub_fdt_get_last_comp_version (fdt) > FDT_SUPPORTED_VERSION)
  247. || (grub_fdt_get_off_dt_struct (fdt) & 0x00000003)
  248. || (grub_fdt_get_size_dt_struct (fdt) & 0x00000003)
  249. || (grub_fdt_get_off_dt_struct (fdt) + grub_fdt_get_size_dt_struct (fdt)
  250. > grub_fdt_get_totalsize (fdt))
  251. || (grub_fdt_get_off_dt_strings (fdt) + grub_fdt_get_size_dt_strings (fdt)
  252. > grub_fdt_get_totalsize (fdt))
  253. || (grub_fdt_get_off_mem_rsvmap (fdt) & 0x00000007)
  254. || (grub_fdt_get_off_mem_rsvmap (fdt)
  255. > grub_fdt_get_totalsize (fdt) - 2 * sizeof(grub_uint64_t)))
  256. return -1;
  257. return 0;
  258. }
  259. int grub_fdt_check_header (const void *fdt, unsigned int size)
  260. {
  261. if (size < sizeof (grub_fdt_header_t)
  262. || (grub_fdt_get_totalsize (fdt) > size)
  263. || grub_fdt_check_header_nosize (fdt) == -1)
  264. return -1;
  265. return 0;
  266. }
  267. static const grub_uint32_t *
  268. advance_token (const void *fdt, const grub_uint32_t *token, const grub_uint32_t *end, int skip_current)
  269. {
  270. for (; token < end; skip_current = 0)
  271. {
  272. switch (grub_be_to_cpu32 (*token))
  273. {
  274. case FDT_BEGIN_NODE:
  275. if (skip_current)
  276. {
  277. token = get_next_node (fdt, (char *) (token + 1));
  278. continue;
  279. }
  280. char *ptr;
  281. for (ptr = (char *) (token + 1); *ptr && ptr < (char *) end; ptr++)
  282. ;
  283. if (ptr >= (char *) end)
  284. return 0;
  285. return token;
  286. case FDT_PROP:
  287. /* Skip property token and following data (len, nameoff and property
  288. value). */
  289. if (token >= end - 1)
  290. return 0;
  291. token += grub_fdt_prop_entry_size(grub_be_to_cpu32(*(token + 1)))
  292. / sizeof(*token);
  293. break;
  294. case FDT_NOP:
  295. token++;
  296. break;
  297. default:
  298. return 0;
  299. }
  300. }
  301. return 0;
  302. }
  303. int grub_fdt_next_node (const void *fdt, unsigned int currentoffset)
  304. {
  305. const grub_uint32_t *token = (const grub_uint32_t *) fdt + (currentoffset + grub_fdt_get_off_dt_struct (fdt)) / 4;
  306. token = advance_token (fdt, token, (const void *) struct_end (fdt), 1);
  307. if (!token)
  308. return -1;
  309. return (int) ((grub_addr_t) token - (grub_addr_t) fdt
  310. - grub_fdt_get_off_dt_struct (fdt));
  311. }
  312. int grub_fdt_first_node (const void *fdt, unsigned int parentoffset)
  313. {
  314. const grub_uint32_t *token, *end;
  315. char *node_name;
  316. if (parentoffset & 0x3)
  317. return -1;
  318. token = (const void *) ((grub_addr_t) fdt + grub_fdt_get_off_dt_struct(fdt)
  319. + parentoffset);
  320. end = (const void *) struct_end (fdt);
  321. if ((token >= end) || (grub_be_to_cpu32(*token) != FDT_BEGIN_NODE))
  322. return -1;
  323. SKIP_NODE_NAME(node_name, token, end);
  324. token = advance_token (fdt, token, end, 0);
  325. if (!token)
  326. return -1;
  327. return (int) ((grub_addr_t) token - (grub_addr_t) fdt
  328. - grub_fdt_get_off_dt_struct (fdt));
  329. }
  330. /* Find a direct sub-node of a given parent node. */
  331. int grub_fdt_find_subnode (const void *fdt, unsigned int parentoffset,
  332. const char *name)
  333. {
  334. const grub_uint32_t *token, *end;
  335. const char *node_name;
  336. int skip_current = 0;
  337. if (parentoffset & 0x3)
  338. return -1;
  339. token = (const void *) ((grub_addr_t) fdt + grub_fdt_get_off_dt_struct(fdt)
  340. + parentoffset);
  341. end = (const void *) struct_end (fdt);
  342. if ((token >= end) || (grub_be_to_cpu32(*token) != FDT_BEGIN_NODE))
  343. return -1;
  344. SKIP_NODE_NAME(node_name, token, end);
  345. while (1) {
  346. token = advance_token (fdt, token, end, skip_current);
  347. if (!token)
  348. return -1;
  349. skip_current = 1;
  350. node_name = (const char *) token + 4;
  351. if (grub_strcmp (node_name, name) == 0)
  352. return (int) ((grub_addr_t) token - (grub_addr_t) fdt
  353. - grub_fdt_get_off_dt_struct (fdt));
  354. }
  355. }
  356. const char *
  357. grub_fdt_get_nodename (const void *fdt, unsigned int nodeoffset)
  358. {
  359. return (const char *) fdt + grub_fdt_get_off_dt_struct(fdt) + nodeoffset + 4;
  360. }
  361. int grub_fdt_add_subnode (void *fdt, unsigned int parentoffset,
  362. const char *name)
  363. {
  364. unsigned int entry_size = node_entry_size(name);
  365. if ((parentoffset & 0x3) || (get_free_space (fdt) < entry_size))
  366. return -1;
  367. /* The new node entry will increase the size of the structure block: rearrange
  368. blocks such that there is sufficient free space between the structure and
  369. the strings block, then add the new node entry. */
  370. if (rearrange_blocks (fdt, entry_size) < 0)
  371. return -1;
  372. return add_subnode (fdt, parentoffset, name);
  373. }
  374. const void *
  375. grub_fdt_get_prop (const void *fdt, unsigned int nodeoffset, const char *name,
  376. grub_uint32_t *len)
  377. {
  378. grub_uint32_t *prop;
  379. if ((nodeoffset >= grub_fdt_get_size_dt_struct (fdt)) || (nodeoffset & 0x3)
  380. || (grub_be_to_cpu32(*(grub_uint32_t *) ((grub_addr_t) fdt
  381. + grub_fdt_get_off_dt_struct (fdt) + nodeoffset))
  382. != FDT_BEGIN_NODE))
  383. return 0;
  384. prop = find_prop (fdt, nodeoffset, name);
  385. if (!prop)
  386. return 0;
  387. if (len)
  388. *len = grub_be_to_cpu32 (*(prop + 1));
  389. return prop + 3;
  390. }
  391. int grub_fdt_set_prop (void *fdt, unsigned int nodeoffset, const char *name,
  392. const void *val, grub_uint32_t len)
  393. {
  394. grub_uint32_t *prop;
  395. int prop_name_present = 0;
  396. grub_uint32_t nameoff = 0;
  397. if ((nodeoffset >= grub_fdt_get_size_dt_struct (fdt)) || (nodeoffset & 0x3)
  398. || (grub_be_to_cpu32(*(grub_uint32_t *) ((grub_addr_t) fdt
  399. + grub_fdt_get_off_dt_struct (fdt) + nodeoffset))
  400. != FDT_BEGIN_NODE))
  401. return -1;
  402. prop = find_prop (fdt, nodeoffset, name);
  403. if (prop)
  404. {
  405. grub_uint32_t prop_len = ALIGN_UP(grub_be_to_cpu32 (*(prop + 1)),
  406. sizeof(grub_uint32_t));
  407. grub_uint32_t i;
  408. prop_name_present = 1;
  409. for (i = 0; i < prop_len / sizeof(grub_uint32_t); i++)
  410. *(prop + 3 + i) = grub_cpu_to_be32_compile_time (FDT_NOP);
  411. if (len > ALIGN_UP(prop_len, sizeof(grub_uint32_t)))
  412. {
  413. /* Length of new property value is greater than the space allocated
  414. for the current value: a new entry needs to be created, so save the
  415. nameoff field of the current entry and replace the current entry
  416. with NOP tokens. */
  417. nameoff = grub_be_to_cpu32 (*(prop + 2));
  418. *prop = *(prop + 1) = *(prop + 2) = grub_cpu_to_be32_compile_time (FDT_NOP);
  419. prop = NULL;
  420. }
  421. }
  422. if (!prop || !prop_name_present) {
  423. unsigned int needed_space = 0;
  424. if (!prop)
  425. needed_space = grub_fdt_prop_entry_size(len);
  426. if (!prop_name_present)
  427. needed_space += grub_strlen (name) + 1;
  428. if (needed_space > get_free_space (fdt))
  429. return -1;
  430. if (rearrange_blocks (fdt, !prop ? grub_fdt_prop_entry_size(len) : 0) < 0)
  431. return -1;
  432. }
  433. if (!prop_name_present) {
  434. /* Append the property name at the end of the strings block. */
  435. nameoff = grub_fdt_get_size_dt_strings (fdt);
  436. grub_strcpy ((char *) fdt + grub_fdt_get_off_dt_strings (fdt) + nameoff,
  437. name);
  438. grub_fdt_set_size_dt_strings (fdt, grub_fdt_get_size_dt_strings (fdt)
  439. + grub_strlen (name) + 1);
  440. }
  441. if (!prop) {
  442. char *node_name = (char *) ((grub_addr_t) fdt
  443. + grub_fdt_get_off_dt_struct (fdt) + nodeoffset
  444. + sizeof(grub_uint32_t));
  445. prop = (void *) (node_name + ALIGN_UP(grub_strlen(node_name) + 1, 4));
  446. grub_memmove (prop + grub_fdt_prop_entry_size(len) / sizeof(*prop), prop,
  447. struct_end(fdt) - (grub_addr_t) prop);
  448. grub_fdt_set_size_dt_struct (fdt, grub_fdt_get_size_dt_struct (fdt)
  449. + grub_fdt_prop_entry_size(len));
  450. *prop = grub_cpu_to_be32_compile_time (FDT_PROP);
  451. *(prop + 2) = grub_cpu_to_be32 (nameoff);
  452. }
  453. *(prop + 1) = grub_cpu_to_be32 (len);
  454. /* Insert padding bytes at the end of the value; if they are not needed, they
  455. will be overwritten by the following memcpy. */
  456. *(prop + grub_fdt_prop_entry_size(len) / sizeof(grub_uint32_t) - 1) = 0;
  457. grub_memcpy (prop + 3, val, len);
  458. return 0;
  459. }
  460. int
  461. grub_fdt_create_empty_tree (void *fdt, unsigned int size)
  462. {
  463. struct grub_fdt_empty_tree *et;
  464. if (size < GRUB_FDT_EMPTY_TREE_SZ)
  465. return -1;
  466. grub_memset (fdt, 0, size);
  467. et = fdt;
  468. et->empty_node.tree_end = grub_cpu_to_be32_compile_time (FDT_END);
  469. et->empty_node.node_end = grub_cpu_to_be32_compile_time (FDT_END_NODE);
  470. et->empty_node.node_start = grub_cpu_to_be32_compile_time (FDT_BEGIN_NODE);
  471. ((struct grub_fdt_empty_tree *) fdt)->header.off_mem_rsvmap =
  472. grub_cpu_to_be32_compile_time (ALIGN_UP (sizeof (grub_fdt_header_t), 8));
  473. grub_fdt_set_off_dt_strings (fdt, sizeof (*et));
  474. grub_fdt_set_off_dt_struct (fdt,
  475. sizeof (et->header) + sizeof (et->empty_rsvmap));
  476. grub_fdt_set_version (fdt, FDT_SUPPORTED_VERSION);
  477. grub_fdt_set_last_comp_version (fdt, FDT_SUPPORTED_VERSION);
  478. grub_fdt_set_size_dt_struct (fdt, sizeof (et->empty_node));
  479. grub_fdt_set_totalsize (fdt, size);
  480. grub_fdt_set_magic (fdt, FDT_MAGIC);
  481. return 0;
  482. }