Coverage Report

Created: 2026-11-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/common/uimage.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * uimage.c - uimage handling code
4
 *
5
 * Copyright (c) 2011 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6
 *
7
 * partly based on U-Boot uImage code
8
 */
9
#include <common.h>
10
#include <image.h>
11
#include <malloc.h>
12
#include <errno.h>
13
#include <crc.h>
14
#include <libbb.h>
15
#include <libfile.h>
16
#include <uncompress.h>
17
#include <fcntl.h>
18
#include <fs.h>
19
#include <rtc.h>
20
#include <filetype.h>
21
#include <memory.h>
22
#include <zero_page.h>
23
24
static inline int uimage_is_multi_image(struct uimage_handle *handle)
25
0
{
26
0
  return (handle->header.ih_type == IH_TYPE_MULTI) ? 1 : 0;
27
0
}
28
29
void uimage_print_contents(struct uimage_handle *handle)
30
0
{
31
0
  struct image_header *hdr = &handle->header;
32
33
0
  printf("   Image Name:   %.*s\n", IH_NMLEN, hdr->ih_name);
34
35
0
  if (IS_ENABLED(CONFIG_TIMESTAMP)) {
36
0
    struct rtc_time tm;
37
38
0
    printf("   Created:      ");
39
0
    to_tm(hdr->ih_time, &tm);
40
0
    printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
41
0
        tm.tm_year, tm.tm_mon, tm.tm_mday,
42
0
        tm.tm_hour, tm.tm_min, tm.tm_sec);
43
0
  }
44
45
#if defined(CONFIG_BOOTM_SHOW_TYPE)
46
  printf("   OS:           %s\n", image_get_os_name(hdr->ih_os));
47
  printf("   Architecture: %s\n", image_get_arch_name(hdr->ih_arch));
48
  printf("   Type:         %s\n", image_get_type_name(hdr->ih_type));
49
  printf("   Compression:  %s\n", image_get_comp_name(hdr->ih_comp));
50
#endif
51
0
  printf("   Data Size:    %d Bytes = %s\n", hdr->ih_size,
52
0
      size_human_readable(hdr->ih_size));
53
0
  printf("   Load Address: %08x\n", hdr->ih_load);
54
0
  printf("   Entry Point:  %08x\n", hdr->ih_ep);
55
56
0
  if (uimage_is_multi_image(handle)) {
57
0
    int i;
58
59
0
    printf("   Contents:\n");
60
61
0
    for (i = 0; i < handle->nb_data_entries; i++) {
62
0
      struct uimage_handle_data *data = &handle->ihd[i];
63
64
0
      printf("      Image %d: %ld (%s)\n", i,
65
0
          data->len, size_human_readable(data->len));
66
0
    }
67
0
  }
68
0
}
69
EXPORT_SYMBOL(uimage_print_contents);
70
71
ssize_t uimage_get_size(struct uimage_handle *handle, unsigned int image_no)
72
0
{
73
0
  if (image_no >= handle->nb_data_entries)
74
0
    return -EINVAL;
75
76
0
  return handle->ihd[image_no].len;
77
0
}
78
EXPORT_SYMBOL(uimage_get_size);
79
80
/*
81
 * open a uimage. This will check the header contents and
82
 * return a handle to the uImage
83
 */
84
struct uimage_handle *uimage_open(const char *filename)
85
0
{
86
0
  int fd;
87
0
  uint32_t checksum;
88
0
  struct uimage_handle *handle;
89
0
  struct image_header *header;
90
0
  int i;
91
0
  int ret;
92
0
  char *copy = NULL;
93
94
0
  if (is_tftp_fs(filename)) {
95
0
    ret = cache_file(filename, &copy);
96
0
    if (ret)
97
0
      return NULL;
98
0
    filename = copy;
99
0
  }
100
101
0
  fd = open(filename, O_RDONLY);
102
0
  if (fd < 0) {
103
0
    printf("could not open: %m\n");
104
0
    free(copy);
105
0
    return NULL;
106
0
  }
107
108
0
  handle = xzalloc(sizeof(struct uimage_handle));
109
0
  header = &handle->header;
110
111
0
  handle->copy = copy;
112
113
0
  if (read_full(fd, header, sizeof(*header)) != sizeof(*header)) {
114
0
    printf("could not read: %m\n");
115
0
    goto err_out;
116
0
  }
117
118
0
  if (uimage_to_cpu(header->ih_magic) != IH_MAGIC) {
119
0
    printf("Bad Magic Number\n");
120
0
    goto err_out;
121
0
  }
122
123
0
  checksum = uimage_to_cpu(header->ih_hcrc);
124
0
  header->ih_hcrc = 0;
125
126
0
  if (crc32(0, header, sizeof(*header)) != checksum) {
127
0
    printf("Bad Header Checksum\n");
128
0
    goto err_out;
129
0
  }
130
131
  /* convert header to cpu native endianness */
132
0
  header->ih_magic = uimage_to_cpu(header->ih_magic);
133
0
  header->ih_hcrc = uimage_to_cpu(header->ih_hcrc);
134
0
  header->ih_time = uimage_to_cpu(header->ih_time);
135
0
  header->ih_size = uimage_to_cpu(header->ih_size);
136
0
  header->ih_load = uimage_to_cpu(header->ih_load);
137
0
  header->ih_ep = uimage_to_cpu(header->ih_ep);
138
0
  header->ih_dcrc = uimage_to_cpu(header->ih_dcrc);
139
140
0
  if (header->ih_name[0]) {
141
0
    handle->name = xzalloc(IH_NMLEN + 1);
142
0
    strncpy(handle->name, header->ih_name, IH_NMLEN);
143
0
  } else {
144
0
    handle->name = xstrdup(filename);
145
0
  }
146
147
0
  if (uimage_is_multi_image(handle)) {
148
0
    size_t offset;
149
150
0
    for (i = 0; i < MAX_MULTI_IMAGE_COUNT; i++) {
151
0
      u32 size;
152
153
0
      ret = read_full(fd, &size, sizeof(size));
154
0
      if (ret != sizeof(size))
155
0
        goto err_out;
156
157
0
      if (!size)
158
0
        break;
159
160
0
      handle->ihd[i].len = uimage_to_cpu(size);
161
0
    }
162
163
0
    handle->nb_data_entries = i;
164
165
    /* offset of the first image in a multifile image */
166
0
    offset = 0;
167
168
0
    for (i = 0; i < handle->nb_data_entries; i++) {
169
0
      handle->ihd[i].offset = offset;
170
0
      offset += (handle->ihd[i].len + 3) & ~3;
171
0
    }
172
173
0
    handle->data_offset = sizeof(struct image_header) +
174
0
      sizeof(u32) * (handle->nb_data_entries + 1);
175
0
  } else {
176
0
    handle->ihd[0].offset = 0;
177
0
    handle->ihd[0].len = header->ih_size;
178
0
    handle->nb_data_entries = 1;
179
0
    handle->data_offset = sizeof(struct image_header);
180
0
  }
181
182
  /*
183
   * fd is now at the first data word
184
   */
185
0
  handle->fd = fd;
186
0
  handle->filename = xstrdup(filename);
187
188
0
  return handle;
189
0
err_out:
190
0
  close(fd);
191
192
0
  free(handle->name);
193
0
  if (handle->copy) {
194
0
    unlink(handle->copy);
195
0
    free(handle->copy);
196
0
  }
197
0
  free(handle);
198
199
0
  return NULL;
200
0
}
201
EXPORT_SYMBOL(uimage_open);
202
203
/*
204
 * close a uImage previously opened with uimage_open
205
 */
206
void uimage_close(struct uimage_handle *handle)
207
0
{
208
0
  close(handle->fd);
209
210
0
  if (handle->copy) {
211
0
    unlink(handle->copy);
212
0
    free(handle->copy);
213
0
  }
214
215
0
  free(handle->name);
216
0
  free(handle->filename);
217
0
  free(handle);
218
0
}
219
EXPORT_SYMBOL(uimage_close);
220
221
static int uimage_fd;
222
223
static long uimage_fill(void *buf, unsigned long len)
224
0
{
225
0
  return read_full(uimage_fd, buf, len);
226
0
}
227
228
static int uncompress_copy(unsigned char *inbuf_unused, long len,
229
    long(*fill)(void*, unsigned long),
230
    long(*flush)(void*, unsigned long),
231
    unsigned char *outbuf_unused,
232
    long *pos,
233
    void(*error_fn)(char *x))
234
0
{
235
0
  int ret;
236
0
  void *buf = xmalloc(PAGE_SIZE);
237
238
0
  while (len) {
239
0
    int now = min_t(long, len, PAGE_SIZE);
240
0
    ret = fill(buf, now);
241
0
    if (ret < 0)
242
0
      goto err;
243
0
    ret = flush(buf, now);
244
0
    if (ret < 0)
245
0
      goto err;
246
0
    len -= now;
247
0
  }
248
249
0
  ret = 0;
250
0
err:
251
0
  free(buf);
252
0
  return ret;
253
0
}
254
255
/*
256
 * Verify the data crc of an uImage
257
 */
258
int uimage_verify(struct uimage_handle *handle)
259
0
{
260
0
  u32 crc = 0;
261
0
  int len, ret;
262
0
  loff_t off;
263
0
  void *buf;
264
265
0
  off = sizeof(struct image_header);
266
0
  if (lseek(handle->fd, off, SEEK_SET) != off)
267
0
    return -errno;
268
269
0
  buf = xmalloc(PAGE_SIZE);
270
271
0
  len = handle->header.ih_size;
272
0
  while (len) {
273
0
    int now = min(len, PAGE_SIZE);
274
0
    ret = read(handle->fd, buf, now);
275
0
    if (ret < 0)
276
0
      goto err;
277
0
    if (!ret)
278
0
      break;
279
0
    crc = crc32(crc, buf, ret);
280
0
    len -= ret;
281
0
  }
282
283
0
  if (crc != handle->header.ih_dcrc) {
284
0
    printf("Bad Data CRC: 0x%08x != 0x%08x\n",
285
0
        crc, handle->header.ih_dcrc);
286
0
    ret = -EINVAL;
287
0
    goto err;
288
0
  }
289
290
0
  ret = 0;
291
0
err:
292
0
  free(buf);
293
294
0
  return ret;
295
0
}
296
EXPORT_SYMBOL(uimage_verify);
297
298
/*
299
 * Load a uimage, flushing output to flush function
300
 */
301
int uimage_load(struct uimage_handle *handle, unsigned int image_no,
302
    long(*flush)(void*, unsigned long))
303
0
{
304
0
  image_header_t *hdr = &handle->header;
305
0
  struct uimage_handle_data *iha;
306
0
  int ret;
307
0
  loff_t off;
308
0
  int (*uncompress_fn)(unsigned char *inbuf, long len,
309
0
        long(*fill)(void*, unsigned long),
310
0
              long(*flush)(void*, unsigned long),
311
0
        unsigned char *output,
312
0
              long *pos,
313
0
        void(*error)(char *x));
314
315
0
  if (image_no >= handle->nb_data_entries)
316
0
    return -EINVAL;
317
318
0
  iha = &handle->ihd[image_no];
319
320
0
  off = iha->offset + handle->data_offset;
321
0
  if (lseek(handle->fd, off, SEEK_SET) != off)
322
0
    return -errno;
323
324
  /* if ramdisk U-Boot expect to ignore the compression type */
325
0
  if (hdr->ih_comp == IH_COMP_NONE || hdr->ih_type == IH_TYPE_RAMDISK)
326
0
    uncompress_fn = uncompress_copy;
327
0
  else
328
0
    uncompress_fn = uncompress;
329
330
0
  uimage_fd = handle->fd;
331
332
0
  ret = uncompress_fn(NULL, iha->len, uimage_fill, flush,
333
0
        NULL, NULL,
334
0
        uncompress_err_stdout);
335
0
  return ret;
336
0
}
337
EXPORT_SYMBOL(uimage_load);
338
339
static void *uimage_buf;
340
static size_t uimage_size;
341
static size_t uimage_skip;
342
static size_t uimage_maxsize;
343
static bool uimage_extract_partial;
344
345
static long uimage_sdram_flush(void *buf, unsigned long len)
346
0
{
347
0
  unsigned long skip_now = 0;
348
349
  /* Skip the first uimage_skip bytes of decompressed data */
350
0
  if (uimage_skip > 0) {
351
0
    skip_now = min_t(unsigned long, uimage_skip, len);
352
0
    buf += skip_now;
353
0
    len -= skip_now;
354
0
    uimage_skip -= skip_now;
355
356
0
    if (len == 0)
357
0
      return skip_now;
358
0
  }
359
360
0
  if (!uimage_extract_partial && uimage_size + len > uimage_maxsize)
361
0
    return -ENOSPC;
362
363
  /* Buffer full: discard excess data in partial mode */
364
0
  if (uimage_size >= uimage_maxsize)
365
0
    return len + skip_now;
366
367
0
  len = min_t(unsigned long, len, uimage_maxsize - uimage_size);
368
369
0
  if (zero_page_contains((unsigned long)uimage_buf + uimage_size))
370
0
    zero_page_memcpy(uimage_buf + uimage_size, buf, len);
371
0
  else
372
0
    memcpy(uimage_buf + uimage_size, buf, len);
373
374
0
  uimage_size += len;
375
376
0
  return len + skip_now;
377
0
}
378
379
/*
380
 * Load an uImage to a fixed buffer
381
 *
382
 * @handle: uImage handle
383
 * @image_no: image number within the uImage
384
 * @load_address: address to load the image to
385
 * @offset: offset in bytes to skip from the beginning of the decompressed data
386
 *
387
 * Return: number of bytes written on success or negative error code
388
 */
389
int uimage_load_into_fixed_buf(struct uimage_handle *handle, int image_no,
390
             void *load_address, size_t size,
391
             loff_t offset, bool partial)
392
0
{
393
0
  int ret;
394
395
0
  uimage_buf = (void *)load_address;
396
0
  uimage_maxsize = size;
397
0
  uimage_size = 0;
398
0
  uimage_skip = offset;
399
0
  uimage_extract_partial = partial;
400
401
0
  ret = uimage_load(handle, image_no, uimage_sdram_flush);
402
0
  if (ret)
403
0
    return ret;
404
405
0
  return uimage_size;
406
0
}
407
EXPORT_SYMBOL(uimage_load_into_fixed_buf);
408
409
void *uimage_load_to_buf(struct uimage_handle *handle, int image_no,
410
    size_t *outsize)
411
0
{
412
0
  u32 size;
413
0
  int ret;
414
0
  loff_t off;
415
0
  struct uimage_handle_data *ihd;
416
0
  char ftbuf[128];
417
0
  enum filetype ft;
418
0
  void *buf;
419
420
0
  if (image_no >= handle->nb_data_entries)
421
0
    return NULL;
422
423
0
  ihd = &handle->ihd[image_no];
424
425
0
  off = ihd->offset + handle->data_offset;
426
0
  if (lseek(handle->fd, off, SEEK_SET) != off)
427
0
    return NULL;
428
429
0
  if (handle->header.ih_comp == IH_COMP_NONE) {
430
0
    buf = malloc(ihd->len);
431
0
    if (!buf)
432
0
      return NULL;
433
434
0
    ret = read_full(handle->fd, buf, ihd->len);
435
0
    if (ret < ihd->len) {
436
0
      free(buf);
437
0
      return NULL;
438
0
    }
439
0
    size = ihd->len;
440
0
    goto out;
441
0
  }
442
443
0
  ret = read_full(handle->fd, ftbuf, 128);
444
0
  if (ret != 128)
445
0
    return NULL;
446
447
0
  ft = file_detect_type(ftbuf, 128);
448
0
  if ((int)ft < 0)
449
0
    return NULL;
450
451
0
  if (ft != filetype_gzip)
452
0
    return NULL;
453
454
0
  off = ihd->offset + handle->data_offset + ihd->len - 4;
455
0
  if (lseek(handle->fd, off, SEEK_SET) != off)
456
0
    return NULL;
457
458
0
  ret = read_full(handle->fd, &size, 4);
459
0
  if (ret != 4)
460
0
    return NULL;
461
462
0
  size = le32_to_cpu(size);
463
464
0
  off = ihd->offset + handle->data_offset;
465
0
  if (lseek(handle->fd, off, SEEK_SET) != off)
466
0
    return NULL;
467
468
0
  buf = malloc(size);
469
0
  ret = uncompress_fd_to_buf(handle->fd, buf, uncompress_err_stdout);
470
0
  if (ret) {
471
0
    free(buf);
472
0
    return NULL;
473
0
  }
474
475
0
out:
476
0
  if (outsize)
477
0
    *outsize = size;
478
479
0
  return buf;
480
0
}