transfer_list.c 15 KB

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