Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/loadable.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
3
#include <loadable.h>
4
#include <malloc.h>
5
#include <memory.h>
6
#include <libfile.h>
7
#include <unistd.h>
8
#include <linux/stat.h>
9
#include <linux/fs.h>
10
#include <linux/sizes.h>
11
#include <zero_page.h>
12
#include <fs.h>
13
14
void loadable_set_name(struct loadable *l, const char *fmt, ...)
15
0
{
16
0
  va_list args;
17
18
0
  free(l->name);
19
20
0
  va_start(args, fmt);
21
0
  l->name = xvasprintf(fmt, args);
22
0
  va_end(args);
23
0
}
24
25
const char *loadable_type_tostr(enum loadable_type type)
26
0
{
27
0
  switch (type) {
28
0
  case LOADABLE_KERNEL:
29
0
    return "kernel";
30
0
  case LOADABLE_INITRD:
31
0
    return "initrd";
32
0
  case LOADABLE_FDT:
33
0
    return "fdt";
34
0
  default:
35
0
    return NULL;
36
0
  }
37
0
}
38
39
/**
40
 * loadable_get_info - obtain metadata without loading data
41
 * @l: loadable
42
 * @info: loadable info populated on success
43
 *
44
 * This function reads the minimum necessary headers/properties
45
 * needed to determine size and addresses.
46
 * Result is cached in loadable->info.
47
 *
48
 * Return: 0 on success, or negative error code otherwise
49
 */
50
int loadable_get_info(struct loadable *l, struct loadable_info *info)
51
0
{
52
0
  int ret;
53
54
0
  if (!l->info_valid) {
55
0
    ret = l->ops->get_info(l, &l->info);
56
0
    if (ret)
57
0
      return ret;
58
0
    l->info_valid = true;
59
0
  }
60
61
0
  *info = l->info;
62
0
  return 0;
63
0
}
64
65
/**
66
 * loadable_extract_into_buf - load fully to target address
67
 * @l: loadable
68
 * @load_addr: virtual address to load data to
69
 * @size: size of buffer at load_addr
70
 * @offset: offset within the loadable to start loading from
71
 * @flags: A bitmask of OR-ed LOADABLE_EXTRACT_ flags
72
 *
73
 * Instructs the loadable implementation to populate the specified
74
 * buffer with data, so it can be reused. This may involve decompression
75
 * if the loadable was constructed by loadable_decompress for example.
76
 *
77
 * if !(@flags & LOADABLE_EXTRACT_PARTIAL), -ENOSPC will be returned if the
78
 * size is too small.
79
 *
80
 * Return: actual number of bytes read on success, negative errno on error
81
 */
82
ssize_t loadable_extract_into_buf(struct loadable *l, void *load_addr,
83
          size_t size, loff_t offset, unsigned flags)
84
0
{
85
0
  return l->ops->extract_into_buf(l, load_addr, size, offset, flags);
86
0
}
87
88
0
#define EXTRACT_FD_BUF_SIZE SZ_1M
89
90
/**
91
 * loadable_extract_into_fd - extract loadable data to a file descriptor
92
 * @l: loadable
93
 * @fd: file descriptor to write to
94
 *
95
 * Extracts data from the loadable and writes it to the specified file
96
 * descriptor. Uses a 1MB buffer and streams data in chunks.
97
 *
98
 * Return: total bytes written on success, negative errno on error
99
 */
100
ssize_t loadable_extract_into_fd(struct loadable *l, int fd)
101
0
{
102
0
  void *buf;
103
0
  loff_t offset = 0;
104
0
  ssize_t total = 0;
105
0
  ssize_t ret;
106
107
0
  buf = malloc(EXTRACT_FD_BUF_SIZE);
108
0
  if (!buf)
109
0
    return -ENOMEM;
110
111
0
  while (1) {
112
0
    ret = loadable_extract_into_buf(l, buf, EXTRACT_FD_BUF_SIZE,
113
0
            offset, LOADABLE_EXTRACT_PARTIAL);
114
0
    if (ret < 0)
115
0
      goto out;
116
0
    if (ret == 0)
117
0
      break;
118
119
0
    ret = write_full(fd, buf, ret);
120
0
    if (ret < 0)
121
0
      goto out;
122
123
0
    offset += ret;
124
0
    total += ret;
125
0
  }
126
127
0
  ret = total;
128
0
out:
129
0
  free(buf);
130
0
  return ret;
131
0
}
132
133
static ssize_t __loadable_extract_into_sdram(struct loadable *l,
134
               unsigned long *adr,
135
               unsigned long *size,
136
               unsigned long end,
137
               loadable_fixup_adr_t fixup_adr)
138
0
{
139
0
  ssize_t nbyts;
140
141
0
  if (fixup_adr) {
142
0
    unsigned long new_adr = fixup_adr(l, *adr);
143
0
    unsigned long gap = new_adr - *adr;
144
145
0
    if (WARN_ON(new_adr < *adr))
146
0
      return -EINVAL;
147
0
    if (gap > *size)
148
0
      return -EBUSY;
149
150
0
    memset((void *)*adr, 0, gap);
151
0
    *adr = new_adr;
152
0
    *size -= gap;
153
0
  }
154
155
0
  nbyts = loadable_extract_into_buf_full(l, (void *)*adr, *size);
156
0
  if (nbyts < 0)
157
0
    return nbyts;
158
159
0
  *adr += nbyts;
160
0
  *size -= nbyts;
161
162
0
  if (*adr - 1 > end)
163
0
    return -EBUSY;
164
165
0
  return nbyts;
166
0
}
167
168
/**
169
 * loadable_extract_into_sdram_all - extract main and chained loadables into SDRAM
170
 * @main: main loadable with optional chained loadables
171
 * @adr: start address of the SDRAM region
172
 * @end: end address of the SDRAM region (inclusive)
173
 * @fixup_adr: optional callback returning the address to extract a chained
174
 *         loadable to, given the address right after its predecessor
175
 *
176
 * Extracts the main loadable and all chained loadables into a contiguous SDRAM
177
 * region. Registers the region with the appropriate memory type based on
178
 * the loadable type. Gaps inserted by @fixup_adr are zero-filled.
179
 *
180
 * Return: SDRAM resource on success, or error pointer on failure
181
 */
182
struct resource *loadable_extract_into_sdram_all(struct loadable *main,
183
             unsigned long adr,
184
             unsigned long end,
185
             loadable_fixup_adr_t fixup_adr)
186
0
{
187
0
  struct loadable *lc;
188
0
  unsigned long adr_orig = adr;
189
0
  unsigned long size = end - adr + 1;
190
0
  struct resource *res;
191
0
  unsigned memtype;
192
0
  unsigned memattrs;
193
0
  ssize_t ret;
194
195
  /* FIXME: EFI payloads are started with MMU enabled, so for now
196
   * we keep attributes as RWX instead of remapping later on
197
   */
198
0
  memattrs = IS_ENABLED(CONFIG_EFI_LOADER) ? MEMATTRS_RWX : MEMATTRS_RW;
199
200
0
  if (main->type == LOADABLE_KERNEL)
201
0
    memtype = MEMTYPE_LOADER_CODE;
202
0
  else
203
0
    memtype = MEMTYPE_LOADER_DATA;
204
205
0
  res = request_sdram_region(loadable_type_tostr(main->type) ?: "image",
206
0
           adr, size, memtype, memattrs);
207
0
  if (!res)
208
0
    return ERR_PTR(-EBUSY);
209
210
0
  ret = __loadable_extract_into_sdram(main, &adr, &size, end, NULL);
211
0
  if (ret < 0)
212
0
    goto err;
213
214
0
  list_for_each_entry(lc, &main->chained_loadables, list) {
215
0
    ret = __loadable_extract_into_sdram(lc, &adr, &size, end,
216
0
                fixup_adr);
217
0
    if (ret < 0)
218
0
      goto err;
219
0
  }
220
221
0
  if (adr == adr_orig) {
222
0
    release_region(res);
223
0
    return NULL;
224
0
  }
225
226
0
  ret = resize_region(res, adr - adr_orig);
227
0
  if (ret)
228
0
    goto err;
229
230
0
  return res;
231
0
err:
232
0
  release_sdram_region(res);
233
0
  return ERR_PTR(ret);
234
0
}
235
236
/**
237
 * loadable_mmap - memory map a buffer
238
 * @l: loadable
239
 * @size: size of memory map on success
240
 *
241
 * This is the most efficient way to access a loadable if supported.
242
 * The returned pointer should be passed to loadable_munmap when no
243
 * longer needed.
244
 *
245
 * Return: read-only pointer to the buffer, NULL for zero-size, or MAP_FAILED on error
246
 */
247
const void *loadable_mmap(struct loadable *l, size_t *size)
248
0
{
249
0
  return l->ops->mmap ? l->ops->mmap(l, size) : MAP_FAILED;
250
0
}
251
252
/**
253
 * loadable_munmap - unmap a buffer
254
 * @l: loadable
255
 * @buf: buffer returned from loadable_mmap
256
 * @size: size of memory map returned by loadable_mmap
257
 */
258
void loadable_munmap(struct loadable *l, const void *buf, size_t size)
259
0
{
260
0
  if (l->ops->munmap)
261
0
    l->ops->munmap(l, buf, size);
262
0
}
263
264
/**
265
 * loadable_extract - extract loadable into newly allocated buffer
266
 * @l: loadable
267
 * @size: on success, set to the number of bytes extracted
268
 *
269
 * Extracts the loadable data into a newly allocated buffer. The caller
270
 * is responsible for freeing the returned buffer with free().
271
 *
272
 * Return: allocated buffer, NULL for zero-size, or error pointer on failure
273
 */
274
void *loadable_extract(struct loadable *l, size_t *size)
275
0
{
276
0
  struct loadable_info li;
277
0
  ssize_t nbytes;
278
0
  void *buf;
279
0
  int ret;
280
281
0
  ret = loadable_get_info(l, &li);
282
0
  if (ret)
283
0
    return ERR_PTR(ret);
284
285
0
  if (li.final_size == LOADABLE_SIZE_UNKNOWN) {
286
0
    if (l->ops->extract)
287
0
      return l->ops->extract(l, size);
288
0
    if (l->ops->mmap) {
289
0
      const void *map = l->ops->mmap(l, size);
290
0
      if (map != MAP_FAILED) {
291
0
        void *dup = memdup(map, *size);
292
0
        if (l->ops->munmap)
293
0
          l->ops->munmap(l, map, *size);
294
0
        return dup;
295
0
      }
296
0
    }
297
298
0
    return ERR_PTR(-EFBIG);
299
0
  }
300
301
  /* We assume extract_into_buf to be more efficient as it
302
   * can allocate once instead of having to resize
303
   */
304
0
  buf = malloc(li.final_size);
305
0
  if (!buf)
306
0
    return ERR_PTR(-ENOMEM);
307
308
  /* We are reading for the full size of the file, so set
309
   * LOADABLE_EXTRACT_PARTIAL to save the underlying loadable
310
   * some work
311
   */
312
0
  nbytes = loadable_extract_into_buf(l, buf, li.final_size, 0,
313
0
             LOADABLE_EXTRACT_PARTIAL);
314
0
  if (nbytes < 0) {
315
0
    free(buf);
316
0
    return ERR_PTR(nbytes);
317
0
  } else if (nbytes == 0) {
318
0
    free(buf);
319
0
    buf = NULL;
320
0
  }
321
322
0
  *size = nbytes;
323
0
  return buf;
324
0
}
325
326
/**
327
 * loadable_view - get read-only view of loadable
328
 * @l: loadable
329
 * @size: on success, set to the size of the view
330
 *
331
 * Use this when you want to non-destructively access the data.
332
 * It tries to find the optimal way to provide the buffer:
333
 * - If memory-mappable, return memory map without allocation
334
 * - If size is known, allocate once and use extract_into_buf
335
 * - If size is unknown, fall back to extract (assumed to be the slowest)
336
 *
337
 * Return: read-only pointer to the buffer, NULL for zero-size, or error pointer on failure
338
 */
339
const void *loadable_view(struct loadable *l, size_t *size)
340
0
{
341
0
  const void *mmap;
342
343
0
  mmap = loadable_mmap(l, size);
344
0
  if (mmap != MAP_FAILED) {
345
0
    l->mmap_active = true;
346
0
    return mmap;
347
0
  }
348
349
0
  l->mmap_active = false;
350
0
  return loadable_extract(l, size);
351
0
}
352
353
/**
354
 * loadable_view_free - free buffer returned by loadable_view()
355
 * @l: loadable
356
 * @buf: buffer returned by loadable_view()
357
 * @size: size returned by loadable_view()
358
 *
359
 * Releases resources associated with a buffer obtained via loadable_view().
360
 */
361
void loadable_view_free(struct loadable *l, const void *buf, size_t size)
362
0
{
363
0
  if (IS_ERR_OR_NULL(buf))
364
0
    return;
365
366
0
  if (l->mmap_active)
367
0
    loadable_munmap(l, buf, size);
368
0
  else
369
0
    free((void *)buf);
370
0
}
371
372
/**
373
 * loadable_chain - chain a loadable to another loadable
374
 * @main: pointer to the main loadable (may be NULL initially)
375
 * @new: loadable to chain
376
 *
377
 * Links @new to @main. If @main is NULL, @new becomes the new main.
378
 * Linked loadables are extracted together by loadable_extract_into_sdram_all().
379
 */
380
void loadable_chain(struct loadable **main, struct loadable *new)
381
0
{
382
0
  if (!new)
383
0
    return;
384
0
  if (!*main) {
385
0
    *main = new;
386
0
    return;
387
0
  }
388
389
0
  list_add_tail(&new->list, &(*main)->chained_loadables);
390
0
  list_splice_tail_init(&new->chained_loadables,
391
0
            &(*main)->chained_loadables);
392
0
}
393
394
/**
395
 * loadable_release - free resources associated with this loadable
396
 * @lp: pointer to loadable (set to NULL on return)
397
 *
398
 * Release resources associated with a loadable and all chained loadables.
399
 *
400
 * This function is a no-op when passed NULL or error pointers.
401
 */
402
void loadable_release(struct loadable **lp)
403
0
{
404
0
  struct loadable *lc, *l, *tmp;
405
406
0
  if (IS_ERR_OR_NULL(lp) || IS_ERR_OR_NULL(*lp))
407
0
    return;
408
409
0
  l = *lp;
410
411
0
  list_for_each_entry_safe(lc, tmp, &l->chained_loadables, list)
412
0
    loadable_release(&lc);
413
414
0
  if (l->ops->release)
415
0
    l->ops->release(l);
416
417
0
  list_del(&l->list);
418
0
  free(l->name);
419
0
  free(l);
420
0
  *lp = NULL;
421
0
}