transfer_list.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (c) 2023, Linaro Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch.h>
  7. #include <assert.h>
  8. #include <inttypes.h>
  9. #include <string.h>
  10. #include <common/debug.h>
  11. #include <lib/transfer_list.h>
  12. #include <lib/utils_def.h>
  13. void transfer_list_dump(struct transfer_list_header *tl)
  14. {
  15. struct transfer_list_entry *te = NULL;
  16. int i = 0;
  17. if (!tl) {
  18. return;
  19. }
  20. INFO("Dump transfer list:\n");
  21. INFO("signature 0x%x\n", tl->signature);
  22. INFO("checksum 0x%x\n", tl->checksum);
  23. INFO("version 0x%x\n", tl->version);
  24. INFO("hdr_size 0x%x\n", tl->hdr_size);
  25. INFO("alignment 0x%x\n", tl->alignment);
  26. INFO("size 0x%x\n", tl->size);
  27. INFO("max_size 0x%x\n", tl->max_size);
  28. INFO("flags 0x%x\n", tl->flags);
  29. while (true) {
  30. te = transfer_list_next(tl, te);
  31. if (!te) {
  32. break;
  33. }
  34. INFO("Entry %d:\n", i++);
  35. INFO("tag_id 0x%x\n", te->tag_id);
  36. INFO("hdr_size 0x%x\n", te->hdr_size);
  37. INFO("data_size 0x%x\n", te->data_size);
  38. INFO("data_addr 0x%lx\n",
  39. (unsigned long)transfer_list_entry_data(te));
  40. }
  41. }
  42. /*******************************************************************************
  43. * Set the handoff arguments according to the transfer list payload
  44. * Return pointer to the entry point info if arguments are set properly
  45. * or NULL if not
  46. ******************************************************************************/
  47. entry_point_info_t *
  48. transfer_list_set_handoff_args(struct transfer_list_header *tl,
  49. entry_point_info_t *ep_info)
  50. {
  51. struct transfer_list_entry *te = NULL;
  52. void *dt = NULL;
  53. if (!ep_info || !tl || transfer_list_check_header(tl) == TL_OPS_NON) {
  54. return NULL;
  55. }
  56. te = transfer_list_find(tl, TL_TAG_FDT);
  57. dt = transfer_list_entry_data(te);
  58. ep_info->args.arg1 = TRANSFER_LIST_SIGNATURE |
  59. REGISTER_CONVENTION_VERSION_MASK;
  60. ep_info->args.arg3 = (uintptr_t)tl;
  61. if (GET_RW(ep_info->spsr) == MODE_RW_32) {
  62. /* aarch32 */
  63. ep_info->args.arg0 = 0;
  64. ep_info->args.arg2 = (uintptr_t)dt;
  65. } else {
  66. /* aarch64 */
  67. ep_info->args.arg0 = (uintptr_t)dt;
  68. ep_info->args.arg2 = 0;
  69. }
  70. return ep_info;
  71. }
  72. /*******************************************************************************
  73. * Creating a transfer list in a reserved memory region specified
  74. * Compliant to 2.4.5 of Firmware handoff specification (v0.9)
  75. * Return pointer to the created transfer list or NULL on error
  76. ******************************************************************************/
  77. struct transfer_list_header *transfer_list_init(void *addr, size_t max_size)
  78. {
  79. struct transfer_list_header *tl = addr;
  80. if (!addr || max_size == 0) {
  81. return NULL;
  82. }
  83. if (!is_aligned((uintptr_t)addr, 1 << TRANSFER_LIST_INIT_MAX_ALIGN) ||
  84. !is_aligned(max_size, 1 << TRANSFER_LIST_INIT_MAX_ALIGN) ||
  85. max_size < sizeof(*tl)) {
  86. return NULL;
  87. }
  88. memset(tl, 0, max_size);
  89. tl->signature = TRANSFER_LIST_SIGNATURE;
  90. tl->version = TRANSFER_LIST_VERSION;
  91. tl->hdr_size = sizeof(*tl);
  92. tl->alignment = TRANSFER_LIST_INIT_MAX_ALIGN; /* initial max align */
  93. tl->size = sizeof(*tl); /* initial size is the size of header */
  94. tl->max_size = max_size;
  95. tl->flags = TL_FLAGS_HAS_CHECKSUM;
  96. transfer_list_update_checksum(tl);
  97. return tl;
  98. }
  99. /*******************************************************************************
  100. * Relocating a transfer list to a reserved memory region specified
  101. * Compliant to 2.4.6 of Firmware handoff specification (v0.9)
  102. * Return pointer to the relocated transfer list or NULL on error
  103. ******************************************************************************/
  104. struct transfer_list_header *
  105. transfer_list_relocate(struct transfer_list_header *tl, void *addr,
  106. size_t max_size)
  107. {
  108. uintptr_t new_addr, align_mask, align_off;
  109. struct transfer_list_header *new_tl;
  110. uint32_t new_max_size;
  111. if (!tl || !addr || max_size == 0) {
  112. return NULL;
  113. }
  114. align_mask = (1 << tl->alignment) - 1;
  115. align_off = (uintptr_t)tl & align_mask;
  116. new_addr = ((uintptr_t)addr & ~align_mask) + align_off;
  117. if (new_addr < (uintptr_t)addr) {
  118. new_addr += (1 << tl->alignment);
  119. }
  120. new_max_size = max_size - (new_addr - (uintptr_t)addr);
  121. /* the new space is not sufficient for the tl */
  122. if (tl->size > new_max_size) {
  123. return NULL;
  124. }
  125. new_tl = (struct transfer_list_header *)new_addr;
  126. memmove(new_tl, tl, tl->size);
  127. new_tl->max_size = new_max_size;
  128. transfer_list_update_checksum(new_tl);
  129. return new_tl;
  130. }
  131. /*******************************************************************************
  132. * Verifying the header of a transfer list
  133. * Compliant to 2.4.1 of Firmware handoff specification (v0.9)
  134. * Return transfer list operation status code
  135. ******************************************************************************/
  136. enum transfer_list_ops
  137. transfer_list_check_header(const struct transfer_list_header *tl)
  138. {
  139. if (!tl) {
  140. return TL_OPS_NON;
  141. }
  142. if (tl->signature != TRANSFER_LIST_SIGNATURE) {
  143. ERROR("Bad transfer list signature %#" PRIx32 "\n",
  144. tl->signature);
  145. return TL_OPS_NON;
  146. }
  147. if (!tl->max_size) {
  148. ERROR("Bad transfer list max size %#" PRIx32 "\n",
  149. tl->max_size);
  150. return TL_OPS_NON;
  151. }
  152. if (tl->size > tl->max_size) {
  153. ERROR("Bad transfer list size %#" PRIx32 "\n", tl->size);
  154. return TL_OPS_NON;
  155. }
  156. if (tl->hdr_size != sizeof(struct transfer_list_header)) {
  157. ERROR("Bad transfer list header size %#" PRIx32 "\n",
  158. tl->hdr_size);
  159. return TL_OPS_NON;
  160. }
  161. if (!transfer_list_verify_checksum(tl)) {
  162. ERROR("Bad transfer list checksum %#" PRIx32 "\n",
  163. tl->checksum);
  164. return TL_OPS_NON;
  165. }
  166. if (tl->version == 0) {
  167. ERROR("Transfer list version is invalid\n");
  168. return TL_OPS_NON;
  169. } else if (tl->version == TRANSFER_LIST_VERSION) {
  170. INFO("Transfer list version is valid for all operations\n");
  171. return TL_OPS_ALL;
  172. } else if (tl->version > TRANSFER_LIST_VERSION) {
  173. INFO("Transfer list version is valid for read-only\n");
  174. return TL_OPS_RO;
  175. }
  176. INFO("Old transfer list version is detected\n");
  177. return TL_OPS_CUS;
  178. }
  179. /*******************************************************************************
  180. * Enumerate the next transfer entry
  181. * Return pointer to the next transfer entry or NULL on error
  182. ******************************************************************************/
  183. struct transfer_list_entry *transfer_list_next(struct transfer_list_header *tl,
  184. struct transfer_list_entry *last)
  185. {
  186. struct transfer_list_entry *te = NULL;
  187. uintptr_t tl_ev = 0;
  188. uintptr_t va = 0;
  189. uintptr_t ev = 0;
  190. size_t sz = 0;
  191. if (!tl) {
  192. return NULL;
  193. }
  194. tl_ev = (uintptr_t)tl + tl->size;
  195. if (last) {
  196. va = (uintptr_t)last;
  197. /* check if the total size overflow */
  198. if (add_overflow(last->hdr_size, last->data_size, &sz)) {
  199. return NULL;
  200. }
  201. /* roundup to the next entry */
  202. if (add_with_round_up_overflow(va, sz, TRANSFER_LIST_GRANULE,
  203. &va)) {
  204. return NULL;
  205. }
  206. } else {
  207. va = (uintptr_t)tl + tl->hdr_size;
  208. }
  209. te = (struct transfer_list_entry *)va;
  210. if (va + sizeof(*te) > tl_ev || te->hdr_size < sizeof(*te) ||
  211. add_overflow(te->hdr_size, te->data_size, &sz) ||
  212. add_overflow(va, sz, &ev) || ev > tl_ev) {
  213. return NULL;
  214. }
  215. return te;
  216. }
  217. /*******************************************************************************
  218. * Calculate the byte sum of a transfer list
  219. * Return byte sum of the transfer list
  220. ******************************************************************************/
  221. static uint8_t calc_byte_sum(const struct transfer_list_header *tl)
  222. {
  223. uint8_t *b = (uint8_t *)tl;
  224. uint8_t cs = 0;
  225. size_t n = 0;
  226. for (n = 0; n < tl->size; n++) {
  227. cs += b[n];
  228. }
  229. return cs;
  230. }
  231. /*******************************************************************************
  232. * Update the checksum of a transfer list
  233. * Return updated checksum of the transfer list
  234. ******************************************************************************/
  235. void transfer_list_update_checksum(struct transfer_list_header *tl)
  236. {
  237. uint8_t cs;
  238. if (!tl || !(tl->flags & TL_FLAGS_HAS_CHECKSUM)) {
  239. return;
  240. }
  241. cs = calc_byte_sum(tl);
  242. cs -= tl->checksum;
  243. cs = 256 - cs;
  244. tl->checksum = cs;
  245. assert(transfer_list_verify_checksum(tl));
  246. }
  247. /*******************************************************************************
  248. * Verify the checksum of a transfer list
  249. * Return true if verified or false if not
  250. ******************************************************************************/
  251. bool transfer_list_verify_checksum(const struct transfer_list_header *tl)
  252. {
  253. if (!tl) {
  254. return false;
  255. }
  256. if (!(tl->flags & TL_FLAGS_HAS_CHECKSUM)) {
  257. return true;
  258. }
  259. return !calc_byte_sum(tl);
  260. }
  261. /*******************************************************************************
  262. * Update the data size of a transfer entry
  263. * Return true on success or false on error
  264. ******************************************************************************/
  265. bool transfer_list_set_data_size(struct transfer_list_header *tl,
  266. struct transfer_list_entry *te,
  267. uint32_t new_data_size)
  268. {
  269. uintptr_t tl_old_ev, new_ev = 0, old_ev = 0, ru_new_ev;
  270. struct transfer_list_entry *dummy_te = NULL;
  271. size_t gap = 0;
  272. size_t mov_dis = 0;
  273. size_t sz = 0;
  274. if (!tl || !te) {
  275. return false;
  276. }
  277. tl_old_ev = (uintptr_t)tl + tl->size;
  278. /*
  279. * calculate the old and new end of TE
  280. * both must be roundup to align with TRANSFER_LIST_GRANULE
  281. */
  282. if (add_overflow(te->hdr_size, te->data_size, &sz) ||
  283. add_with_round_up_overflow((uintptr_t)te, sz, TRANSFER_LIST_GRANULE,
  284. &old_ev)) {
  285. return false;
  286. }
  287. if (add_overflow(te->hdr_size, new_data_size, &sz) ||
  288. add_with_round_up_overflow((uintptr_t)te, sz, TRANSFER_LIST_GRANULE,
  289. &new_ev)) {
  290. return false;
  291. }
  292. if (new_ev > old_ev) {
  293. /*
  294. * move distance should be roundup
  295. * to meet the requirement of TE data max alignment
  296. * ensure that the increased size doesn't exceed
  297. * the max size of TL
  298. */
  299. mov_dis = new_ev - old_ev;
  300. if (round_up_overflow(mov_dis, 1 << tl->alignment, &mov_dis) ||
  301. tl->size + mov_dis > tl->max_size) {
  302. return false;
  303. }
  304. ru_new_ev = old_ev + mov_dis;
  305. memmove((void *)ru_new_ev, (void *)old_ev, tl_old_ev - old_ev);
  306. tl->size += mov_dis;
  307. gap = ru_new_ev - new_ev;
  308. } else {
  309. gap = old_ev - new_ev;
  310. }
  311. if (gap >= sizeof(*dummy_te)) {
  312. /* create a dummy TE to fill up the gap */
  313. dummy_te = (struct transfer_list_entry *)new_ev;
  314. dummy_te->tag_id = TL_TAG_EMPTY;
  315. dummy_te->reserved0 = 0;
  316. dummy_te->hdr_size = sizeof(*dummy_te);
  317. dummy_te->data_size = gap - sizeof(*dummy_te);
  318. }
  319. te->data_size = new_data_size;
  320. transfer_list_update_checksum(tl);
  321. return true;
  322. }
  323. /*******************************************************************************
  324. * Remove a specified transfer entry from a transfer list
  325. * Return true on success or false on error
  326. ******************************************************************************/
  327. bool transfer_list_rem(struct transfer_list_header *tl,
  328. struct transfer_list_entry *te)
  329. {
  330. if (!tl || !te || (uintptr_t)te > (uintptr_t)tl + tl->size) {
  331. return false;
  332. }
  333. te->tag_id = TL_TAG_EMPTY;
  334. te->reserved0 = 0;
  335. transfer_list_update_checksum(tl);
  336. return true;
  337. }
  338. /*******************************************************************************
  339. * Add a new transfer entry into a transfer list
  340. * Compliant to 2.4.3 of Firmware handoff specification (v0.9)
  341. * Return pointer to the added transfer entry or NULL on error
  342. ******************************************************************************/
  343. struct transfer_list_entry *transfer_list_add(struct transfer_list_header *tl,
  344. uint16_t tag_id,
  345. uint32_t data_size,
  346. const void *data)
  347. {
  348. uintptr_t max_tl_ev, tl_ev, ev;
  349. struct transfer_list_entry *te = NULL;
  350. uint8_t *te_data = NULL;
  351. size_t sz = 0;
  352. if (!tl) {
  353. return NULL;
  354. }
  355. max_tl_ev = (uintptr_t)tl + tl->max_size;
  356. tl_ev = (uintptr_t)tl + tl->size;
  357. ev = tl_ev;
  358. /*
  359. * skip the step 1 (optional step)
  360. * new TE will be added into the tail
  361. */
  362. if (add_overflow(sizeof(*te), data_size, &sz) ||
  363. add_with_round_up_overflow(ev, sz, TRANSFER_LIST_GRANULE, &ev) ||
  364. ev > max_tl_ev) {
  365. return NULL;
  366. }
  367. te = (struct transfer_list_entry *)tl_ev;
  368. te->tag_id = tag_id;
  369. te->reserved0 = 0;
  370. te->hdr_size = sizeof(*te);
  371. te->data_size = data_size;
  372. tl->size += ev - tl_ev;
  373. if (data) {
  374. /* get TE data pointer */
  375. te_data = transfer_list_entry_data(te);
  376. if (!te_data) {
  377. return NULL;
  378. }
  379. memmove(te_data, data, data_size);
  380. }
  381. transfer_list_update_checksum(tl);
  382. return te;
  383. }
  384. /*******************************************************************************
  385. * Add a new transfer entry into a transfer list with specified new data
  386. * alignment requirement
  387. * Compliant to 2.4.4 of Firmware handoff specification (v0.9)
  388. * Return pointer to the added transfer entry or NULL on error
  389. ******************************************************************************/
  390. struct transfer_list_entry *
  391. transfer_list_add_with_align(struct transfer_list_header *tl, uint16_t tag_id,
  392. uint32_t data_size, const void *data,
  393. uint8_t alignment)
  394. {
  395. struct transfer_list_entry *te = NULL;
  396. uintptr_t tl_ev, ev, new_tl_ev;
  397. size_t dummy_te_data_sz = 0;
  398. if (!tl) {
  399. return NULL;
  400. }
  401. tl_ev = (uintptr_t)tl + tl->size;
  402. ev = tl_ev + sizeof(struct transfer_list_entry);
  403. if (!is_aligned(ev, 1 << alignment)) {
  404. /*
  405. * TE data address is not aligned to the new alignment
  406. * fill the gap with an empty TE as a placeholder before
  407. * adding the desire TE
  408. */
  409. new_tl_ev = round_up(ev, 1 << alignment) -
  410. sizeof(struct transfer_list_entry);
  411. dummy_te_data_sz =
  412. new_tl_ev - tl_ev - sizeof(struct transfer_list_entry);
  413. if (!transfer_list_add(tl, TL_TAG_EMPTY, dummy_te_data_sz,
  414. NULL)) {
  415. return NULL;
  416. }
  417. }
  418. te = transfer_list_add(tl, tag_id, data_size, data);
  419. if (alignment > tl->alignment) {
  420. tl->alignment = alignment;
  421. transfer_list_update_checksum(tl);
  422. }
  423. return te;
  424. }
  425. /*******************************************************************************
  426. * Search for an existing transfer entry with the specified tag id from a
  427. * transfer list
  428. * Return pointer to the found transfer entry or NULL on error
  429. ******************************************************************************/
  430. struct transfer_list_entry *transfer_list_find(struct transfer_list_header *tl,
  431. uint16_t tag_id)
  432. {
  433. struct transfer_list_entry *te = NULL;
  434. do {
  435. te = transfer_list_next(tl, te);
  436. } while (te && (te->tag_id != tag_id || te->reserved0 != 0));
  437. return te;
  438. }
  439. /*******************************************************************************
  440. * Retrieve the data pointer of a specified transfer entry
  441. * Return pointer to the transfer entry data or NULL on error
  442. ******************************************************************************/
  443. void *transfer_list_entry_data(struct transfer_list_entry *entry)
  444. {
  445. if (!entry) {
  446. return NULL;
  447. }
  448. return (uint8_t *)entry + entry->hdr_size;
  449. }