blob: b33035078c69ded8bb9d515d0a44e41a6598e889 [file] [log] [blame]
Manish Pandey65fe3642025-03-21 12:44:42 +00001/*
2 * Copyright The Transfer List Library Contributors
3 *
4 * SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5 */
6
7#include <assert.h>
8#include <inttypes.h>
9#include <stdio.h>
10#include <string.h>
11
12#include <math_utils.h>
13#include <transfer_list.h>
14
Govindraj Raja442c09a2025-04-10 16:30:22 +020015void transfer_list_dump(struct transfer_list_header *tl)
16{
Manish Pandey65fe3642025-03-21 12:44:42 +000017 struct transfer_list_entry *te = NULL;
18 int i = 0;
19
20 if (!tl) {
21 return;
22 }
23 printf("Dump transfer list:\n");
24 printf("signature 0x%x\n", tl->signature);
25 printf("checksum 0x%x\n", tl->checksum);
26 printf("version 0x%x\n", tl->version);
27 printf("hdr_size 0x%x\n", tl->hdr_size);
28 printf("alignment 0x%x\n", tl->alignment);
29 printf("size 0x%x\n", tl->size);
30 printf("max_size 0x%x\n", tl->max_size);
31 printf("flags 0x%x\n", tl->flags);
32 while (true) {
33 te = transfer_list_next(tl, te);
34 if (!te) {
35 break;
36 }
Harrison Mutaidb83bfa2025-03-21 15:24:55 +000037
Manish Pandey65fe3642025-03-21 12:44:42 +000038 printf("Entry %d:\n", i++);
Harrison Mutaidb83bfa2025-03-21 15:24:55 +000039 transfer_entry_dump(te);
40 }
41}
42
43void transfer_entry_dump(struct transfer_list_entry *te)
44{
45 if (te) {
Manish Pandey65fe3642025-03-21 12:44:42 +000046 printf("tag_id 0x%x\n", te->tag_id);
47 printf("hdr_size 0x%x\n", te->hdr_size);
48 printf("data_size 0x%x\n", te->data_size);
49 printf("data_addr 0x%lx\n",
Harrison Mutaidb83bfa2025-03-21 15:24:55 +000050 (unsigned long)transfer_list_entry_data(te));
Manish Pandey65fe3642025-03-21 12:44:42 +000051 }
52}
53
54/*******************************************************************************
55 * Creating a transfer list in a reserved memory region specified
56 * Compliant to 2.4.5 of Firmware handoff specification (v0.9)
57 * Return pointer to the created transfer list or NULL on error
58 ******************************************************************************/
59struct transfer_list_header *transfer_list_init(void *addr, size_t max_size)
60{
61 struct transfer_list_header *tl = addr;
62
63 if (!addr || max_size == 0) {
64 return NULL;
65 }
66
67 if (!is_aligned((uintptr_t)addr, 1 << TRANSFER_LIST_INIT_MAX_ALIGN) ||
68 !is_aligned(max_size, 1 << TRANSFER_LIST_INIT_MAX_ALIGN) ||
69 max_size < sizeof(*tl)) {
70 return NULL;
71 }
72
73 memset(tl, 0, max_size);
74 tl->signature = TRANSFER_LIST_SIGNATURE;
75 tl->version = TRANSFER_LIST_VERSION;
76 tl->hdr_size = sizeof(*tl);
77 tl->alignment = TRANSFER_LIST_INIT_MAX_ALIGN; /* initial max align */
78 tl->size = sizeof(*tl); /* initial size is the size of header */
79 tl->max_size = max_size;
80 tl->flags = TL_FLAGS_HAS_CHECKSUM;
81
82 transfer_list_update_checksum(tl);
83
84 return tl;
85}
86
87/*******************************************************************************
88 * Relocating a transfer list to a reserved memory region specified
89 * Compliant to 2.4.6 of Firmware handoff specification (v0.9)
90 * Return pointer to the relocated transfer list or NULL on error
91 ******************************************************************************/
92struct transfer_list_header *
93transfer_list_relocate(struct transfer_list_header *tl, void *addr,
94 size_t max_size)
95{
96 uintptr_t new_addr, align_mask, align_off;
97 struct transfer_list_header *new_tl;
98 uint32_t new_max_size;
99
100 if (!tl || !addr || max_size == 0) {
101 return NULL;
102 }
103
104 align_mask = (1 << tl->alignment) - 1;
105 align_off = (uintptr_t)tl & align_mask;
106 new_addr = ((uintptr_t)addr & ~align_mask) + align_off;
107
108 if (new_addr < (uintptr_t)addr) {
109 new_addr += (1 << tl->alignment);
110 }
111
112 new_max_size = max_size - (new_addr - (uintptr_t)addr);
113
114 /* the new space is not sufficient for the tl */
115 if (tl->size > new_max_size) {
116 return NULL;
117 }
118
119 new_tl = (struct transfer_list_header *)new_addr;
120 memmove(new_tl, tl, tl->size);
121 new_tl->max_size = new_max_size;
122
123 transfer_list_update_checksum(new_tl);
124
125 return new_tl;
126}
127
128/*******************************************************************************
129 * Verifying the header of a transfer list
130 * Compliant to 2.4.1 of Firmware handoff specification (v0.9)
131 * Return transfer list operation status code
132 ******************************************************************************/
133enum transfer_list_ops
134transfer_list_check_header(const struct transfer_list_header *tl)
135{
136 if (!tl) {
137 return TL_OPS_NON;
138 }
139
140 if (tl->signature != TRANSFER_LIST_SIGNATURE) {
141 printf("Bad transfer list signature %#" PRIx32 "\n",
142 tl->signature);
143 return TL_OPS_NON;
144 }
145
146 if (!tl->max_size) {
147 printf("Bad transfer list max size %#" PRIx32 "\n",
148 tl->max_size);
149 return TL_OPS_NON;
150 }
151
152 if (tl->size > tl->max_size) {
153 printf("Bad transfer list size %#" PRIx32 "\n", tl->size);
154 return TL_OPS_NON;
155 }
156
157 if (tl->hdr_size != sizeof(struct transfer_list_header)) {
158 printf("Bad transfer list header size %#" PRIx32 "\n",
159 tl->hdr_size);
160 return TL_OPS_NON;
161 }
162
163 if (!transfer_list_verify_checksum(tl)) {
164 printf("Bad transfer list checksum %#" PRIx32 "\n",
165 tl->checksum);
166 return TL_OPS_NON;
167 }
168
169 if (tl->version == 0) {
170 printf("Transfer list version is invalid\n");
171 return TL_OPS_NON;
172 } else if (tl->version == TRANSFER_LIST_VERSION) {
173 printf("Transfer list version is valid for all operations\n");
174 return TL_OPS_ALL;
175 } else if (tl->version > TRANSFER_LIST_VERSION) {
176 printf("Transfer list version is valid for read-only\n");
177 return TL_OPS_RO;
178 }
179
180 printf("Old transfer list version is detected\n");
181 return TL_OPS_CUS;
182}
183
184/*******************************************************************************
185 * Enumerate the next transfer entry
186 * Return pointer to the next transfer entry or NULL on error
187 ******************************************************************************/
188struct transfer_list_entry *transfer_list_next(struct transfer_list_header *tl,
189 struct transfer_list_entry *last)
190{
191 struct transfer_list_entry *te = NULL;
192 uintptr_t tl_ev = 0;
193 uintptr_t va = 0;
194 uintptr_t ev = 0;
195 size_t sz = 0;
196
197 if (!tl) {
198 return NULL;
199 }
200
201 tl_ev = (uintptr_t)tl + tl->size;
202
203 if (last) {
204 va = (uintptr_t)last;
205 /* check if the total size overflow */
206 if (add_overflow(last->hdr_size, last->data_size, &sz)) {
207 return NULL;
208 }
209 /* roundup to the next entry */
210 if (add_with_round_up_overflow(va, sz, TRANSFER_LIST_GRANULE,
211 &va)) {
212 return NULL;
213 }
214 } else {
215 va = (uintptr_t)tl + tl->hdr_size;
216 }
217
218 te = (struct transfer_list_entry *)va;
219
220 if (va + sizeof(*te) > tl_ev || te->hdr_size < sizeof(*te) ||
221 add_overflow(te->hdr_size, te->data_size, &sz) ||
222 add_overflow(va, sz, &ev) || ev > tl_ev) {
223 return NULL;
224 }
225
226 return te;
227}
228
229/*******************************************************************************
Yeoreum Yunb7dc0de2025-06-02 20:38:19 +0100230 * Enumerate the prev transfer entry
231 * Return pointer to the prev transfer entry or NULL on error
232 ******************************************************************************/
233struct transfer_list_entry *transfer_list_prev(struct transfer_list_header *tl,
234 struct transfer_list_entry *last)
235{
236 struct transfer_list_entry *prev;
237 struct transfer_list_entry *te = NULL;
238
239 if (!last || !tl || (tl + tl->hdr_size == (void *)last)) {
240 return NULL;
241 }
242
243 do {
244 prev = te;
245 te = transfer_list_next(tl, te);
246 } while (te && te != last);
247
248 return (te != NULL) ? prev : NULL;
249}
250
251/*******************************************************************************
Manish Pandey65fe3642025-03-21 12:44:42 +0000252 * Calculate the byte sum of a transfer list
253 * Return byte sum of the transfer list
254 ******************************************************************************/
255static uint8_t calc_byte_sum(const struct transfer_list_header *tl)
256{
257 uint8_t *b = (uint8_t *)tl;
258 uint8_t cs = 0;
259 size_t n = 0;
260
261 for (n = 0; n < tl->size; n++) {
262 cs += b[n];
263 }
264
265 return cs;
266}
267
268/*******************************************************************************
269 * Update the checksum of a transfer list
270 * Return updated checksum of the transfer list
271 ******************************************************************************/
272void transfer_list_update_checksum(struct transfer_list_header *tl)
273{
274 uint8_t cs;
275
276 if (!tl || !(tl->flags & TL_FLAGS_HAS_CHECKSUM)) {
277 return;
278 }
279
280 cs = calc_byte_sum(tl);
281 cs -= tl->checksum;
282 cs = 256 - cs;
283 tl->checksum = cs;
284 assert(transfer_list_verify_checksum(tl));
285}
286
287/*******************************************************************************
288 * Verify the checksum of a transfer list
289 * Return true if verified or false if not
290 ******************************************************************************/
291bool transfer_list_verify_checksum(const struct transfer_list_header *tl)
292{
293 if (!tl) {
294 return false;
295 }
296
297 if (!(tl->flags & TL_FLAGS_HAS_CHECKSUM)) {
298 return true;
299 }
300
301 return !calc_byte_sum(tl);
302}
303
304/*******************************************************************************
305 * Update the data size of a transfer entry
306 * Return true on success or false on error
307 ******************************************************************************/
308bool transfer_list_set_data_size(struct transfer_list_header *tl,
309 struct transfer_list_entry *te,
310 uint32_t new_data_size)
311{
Yeoreum Yunb7dc0de2025-06-02 20:38:19 +0100312 uintptr_t tl_old_ev, new_ev = 0, old_ev = 0, merge_ev, ru_new_ev;
Manish Pandey65fe3642025-03-21 12:44:42 +0000313 struct transfer_list_entry *dummy_te = NULL;
314 size_t gap = 0;
315 size_t mov_dis = 0;
316 size_t sz = 0;
317
318 if (!tl || !te) {
319 return false;
320 }
321 tl_old_ev = (uintptr_t)tl + tl->size;
322
323 /*
324 * calculate the old and new end of TE
325 * both must be roundup to align with TRANSFER_LIST_GRANULE
326 */
327 if (add_overflow(te->hdr_size, te->data_size, &sz) ||
328 add_with_round_up_overflow((uintptr_t)te, sz, TRANSFER_LIST_GRANULE,
329 &old_ev)) {
330 return false;
331 }
332 if (add_overflow(te->hdr_size, new_data_size, &sz) ||
333 add_with_round_up_overflow((uintptr_t)te, sz, TRANSFER_LIST_GRANULE,
334 &new_ev)) {
335 return false;
336 }
337
338 if (new_ev > old_ev) {
339 /*
Yeoreum Yunb7dc0de2025-06-02 20:38:19 +0100340 * When next transfer list is dummy,
341 * - if te can be extended in boundary of dummy entry,
342 * extend it to dummy entry and set new dummy entry.
343 *
344 * - otherwise, merge dummy entry with existing te and
345 * extend transfer list as much as it requires.
346 */
347 dummy_te = transfer_list_next(tl, te);
348 if (dummy_te && (dummy_te->tag_id == TL_TAG_EMPTY)) {
349 merge_ev = align_up(old_ev + dummy_te->hdr_size +
350 dummy_te->data_size,
351 TRANSFER_LIST_GRANULE);
352 if (merge_ev >= new_ev) {
353 gap = merge_ev - new_ev;
354 goto set_dummy;
355 } else {
356 old_ev = merge_ev;
357 }
358 }
359
360 /*
Manish Pandey65fe3642025-03-21 12:44:42 +0000361 * move distance should be roundup
362 * to meet the requirement of TE data max alignment
363 * ensure that the increased size doesn't exceed
364 * the max size of TL
365 */
366 mov_dis = new_ev - old_ev;
367 if (round_up_overflow(mov_dis, 1 << tl->alignment, &mov_dis) ||
368 tl->size + mov_dis > tl->max_size) {
369 return false;
370 }
371 ru_new_ev = old_ev + mov_dis;
372 memmove((void *)ru_new_ev, (void *)old_ev, tl_old_ev - old_ev);
373 tl->size += mov_dis;
374 gap = ru_new_ev - new_ev;
375 } else {
376 gap = old_ev - new_ev;
377 }
378
Yeoreum Yunb7dc0de2025-06-02 20:38:19 +0100379set_dummy:
Manish Pandey65fe3642025-03-21 12:44:42 +0000380 if (gap >= sizeof(*dummy_te)) {
381 /* create a dummy TE to fill up the gap */
382 dummy_te = (struct transfer_list_entry *)new_ev;
383 dummy_te->tag_id = TL_TAG_EMPTY;
384 dummy_te->hdr_size = sizeof(*dummy_te);
385 dummy_te->data_size = gap - sizeof(*dummy_te);
386 }
387
388 te->data_size = new_data_size;
389
390 transfer_list_update_checksum(tl);
391 return true;
392}
393
394/*******************************************************************************
395 * Remove a specified transfer entry from a transfer list
396 * Return true on success or false on error
397 ******************************************************************************/
398bool transfer_list_rem(struct transfer_list_header *tl,
399 struct transfer_list_entry *te)
400{
Yeoreum Yunb7dc0de2025-06-02 20:38:19 +0100401 struct transfer_list_entry *prev;
402 struct transfer_list_entry *next;
403
Manish Pandey65fe3642025-03-21 12:44:42 +0000404 if (!tl || !te || (uintptr_t)te > (uintptr_t)tl + tl->size) {
405 return false;
406 }
Yeoreum Yunb7dc0de2025-06-02 20:38:19 +0100407
408 prev = transfer_list_prev(tl, te);
409 next = transfer_list_next(tl, te);
410
411 if (prev && prev->tag_id == TL_TAG_EMPTY) {
412 prev->data_size += align_up(te->hdr_size + te->data_size,
413 TRANSFER_LIST_GRANULE);
414 te = prev;
415 }
416
417 if (next && next->tag_id == TL_TAG_EMPTY) {
418 te->data_size += align_up(next->hdr_size + next->data_size,
419 TRANSFER_LIST_GRANULE);
420 }
421
Manish Pandey65fe3642025-03-21 12:44:42 +0000422 te->tag_id = TL_TAG_EMPTY;
423 transfer_list_update_checksum(tl);
424 return true;
425}
426
427/*******************************************************************************
428 * Add a new transfer entry into a transfer list
429 * Compliant to 2.4.3 of Firmware handoff specification (v0.9)
430 * Return pointer to the added transfer entry or NULL on error
431 ******************************************************************************/
432struct transfer_list_entry *transfer_list_add(struct transfer_list_header *tl,
433 uint32_t tag_id,
434 uint32_t data_size,
435 const void *data)
436{
Harrison Mutai7fb41222025-04-16 14:18:46 +0000437 uintptr_t tl_ev;
Manish Pandey65fe3642025-03-21 12:44:42 +0000438 struct transfer_list_entry *te = NULL;
439 uint8_t *te_data = NULL;
440 uintptr_t te_end;
Manish Pandey65fe3642025-03-21 12:44:42 +0000441
442 if (!tl || (tag_id & (1 << 24))) {
443 return NULL;
444 }
445
446 /*
447 * skip the step 1 (optional step)
448 * new TE will be added into the tail
449 */
450 tl_ev = (uintptr_t)tl + tl->size;
451 te = (struct transfer_list_entry *)align_up(tl_ev,
452 TRANSFER_LIST_GRANULE);
453
454 te_end = align_up((uintptr_t)te + sizeof(*te) + data_size,
455 TRANSFER_LIST_GRANULE);
456
457 if (te_end > (uintptr_t)tl + tl->max_size) {
458 return NULL;
459 }
460
461 te->tag_id = tag_id;
462 te->hdr_size = sizeof(*te);
463 te->data_size = data_size;
464 tl->size += te_end - tl_ev;
465
466 if (data) {
467 /* get TE data pointer */
468 te_data = transfer_list_entry_data(te);
469 if (!te_data) {
470 return NULL;
471 }
472 memmove(te_data, data, data_size);
473 }
474
475 transfer_list_update_checksum(tl);
476
477 return te;
478}
479
480/*******************************************************************************
481 * Add a new transfer entry into a transfer list with specified new data
482 * alignment requirement
483 * Compliant to 2.4.4 of Firmware handoff specification (v0.9)
484 * Return pointer to the added transfer entry or NULL on error
485 ******************************************************************************/
486struct transfer_list_entry *
487transfer_list_add_with_align(struct transfer_list_header *tl, uint32_t tag_id,
488 uint32_t data_size, const void *data,
489 uint8_t alignment)
490{
491 struct transfer_list_entry *te = NULL;
492 uintptr_t tl_ev, ev, new_tl_ev;
493 size_t dummy_te_data_sz = 0;
494
495 if (!tl) {
496 return NULL;
497 }
498
499 tl_ev = (uintptr_t)tl + tl->size;
500 ev = tl_ev + sizeof(struct transfer_list_entry);
501
502 if (!is_aligned(ev, 1 << alignment)) {
503 /*
504 * TE data address is not aligned to the new alignment
505 * fill the gap with an empty TE as a placeholder before
506 * adding the desire TE
507 */
508 new_tl_ev = align_up(ev, 1 << alignment) -
509 sizeof(struct transfer_list_entry);
510 dummy_te_data_sz =
511 new_tl_ev - tl_ev - sizeof(struct transfer_list_entry);
512 if (!transfer_list_add(tl, TL_TAG_EMPTY, dummy_te_data_sz,
513 NULL)) {
514 return NULL;
515 }
516 }
517
518 te = transfer_list_add(tl, tag_id, data_size, data);
519
520 if (alignment > tl->alignment) {
521 tl->alignment = alignment;
522 transfer_list_update_checksum(tl);
523 }
524
525 return te;
526}
527
528/*******************************************************************************
529 * Search for an existing transfer entry with the specified tag id from a
530 * transfer list
531 * Return pointer to the found transfer entry or NULL on error
532 ******************************************************************************/
533struct transfer_list_entry *transfer_list_find(struct transfer_list_header *tl,
534 uint32_t tag_id)
535{
536 struct transfer_list_entry *te = NULL;
537
538 do {
539 te = transfer_list_next(tl, te);
540 } while (te && (te->tag_id != tag_id));
541
542 return te;
543}
544
545/*******************************************************************************
546 * Retrieve the data pointer of a specified transfer entry
547 * Return pointer to the transfer entry data or NULL on error
548 ******************************************************************************/
549void *transfer_list_entry_data(struct transfer_list_entry *entry)
550{
551 if (!entry) {
552 return NULL;
553 }
554 return (uint8_t *)entry + entry->hdr_size;
555}
Harrison Mutaidb83bfa2025-03-21 15:24:55 +0000556
557/*******************************************************************************
558 * Verifies that the transfer list has not already been initialized, then
559 * initializes it at the specified memory location.
560 *
561 * Return pointer to the transfer list or NULL on error
562 * *****************************************************************************/
563struct transfer_list_header *transfer_list_ensure(void *addr, size_t size)
564{
565 struct transfer_list_header *tl = NULL;
566
567 if (transfer_list_check_header(addr) == TL_OPS_ALL) {
568 return (struct transfer_list_header *)addr;
569 }
570
571 tl = transfer_list_init((void *)addr, size);
572
573 return tl;
574}