Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/fs/cramfs/cramfs.c
Line
Count
Source
1
/*
2
 * cramfs.c
3
 *
4
 * Copyright (C) 1999 Linus Torvalds
5
 *
6
 * Copyright (C) 2000-2002 Transmeta Corporation
7
 *
8
 * Copyright (C) 2003 Kai-Uwe Bloem,
9
 * Auerswald GmbH & Co KG, <linux-development@auerswald.de>
10
 * - adapted from the www.tuxbox.org barebox tree, added "ls" command
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License (Version 2) as
14
 * published by the Free Software Foundation.
15
 *
16
 * Compressed ROM filesystem for Linux.
17
 *
18
 * TODO:
19
 * add support for resolving symbolic links
20
 */
21
22
/*
23
 * These are the VFS interfaces to the compressed ROM filesystem.
24
 * The actual compression is based on zlib, see the other files.
25
 */
26
27
#include <common.h>
28
#include <malloc.h>
29
#include <driver.h>
30
#include <init.h>
31
#include <errno.h>
32
#include <fs.h>
33
#include <xfuncs.h>
34
35
#include <asm/byteorder.h>
36
#include <linux/stat.h>
37
#include <linux/magic.h>
38
#include <cramfs/cramfs_fs.h>
39
40
/* These two macros may change in future, to provide better st_ino
41
   semantics. */
42
#define CRAMINO(x)  (CRAMFS_GET_OFFSET(x) ? CRAMFS_GET_OFFSET(x)<<2 : 1)
43
0
#define OFFSET(x) ((x)->i_ino)
44
45
struct cramfs_priv {
46
  struct cramfs_super super;
47
  int curr_base;
48
  char buf[4096];
49
  int curr_block_len;
50
  struct cdev *cdev;
51
};
52
53
struct cramfs_inode_info {
54
  struct inode i_inode;
55
  struct cramfs_inode inode;
56
  unsigned long *block_ptrs;
57
};
58
59
static inline struct cramfs_inode_info* to_cramfs_inode_info(struct inode *inode)
60
0
{
61
0
  return container_of(inode, struct cramfs_inode_info, i_inode);
62
0
}
63
64
static int cramfs_read_super(struct cramfs_priv *priv)
65
0
{
66
0
  unsigned long root_offset;
67
0
  struct cramfs_super *super = &priv->super;
68
0
  struct cdev *cdev = priv->cdev;
69
70
0
  if (cdev_read(cdev, super, sizeof (struct cramfs_super), 0, 0) < sizeof (struct cramfs_super)) {
71
0
    printf("read superblock failed\n");
72
0
    return -EINVAL;
73
0
  }
74
75
  /* Do sanity checks on the superblock */
76
0
  if (super->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
77
    /* check at 512 byte offset */
78
0
    if (cdev_read(cdev, super, sizeof (struct cramfs_super), 512, 0) < sizeof (struct cramfs_super)) {
79
0
      printf("read superblock failed\n");
80
0
      return -EINVAL;
81
0
    }
82
0
    if (super->magic != CRAMFS_32 (CRAMFS_MAGIC)) {
83
0
      printf ("cramfs: wrong magic\n");
84
0
      return -1;
85
0
    }
86
0
  }
87
88
  /* flags is reused several times, so swab it once */
89
0
  super->flags = CRAMFS_32 (super->flags);
90
0
  super->size = CRAMFS_32 (super->size);
91
92
  /* get feature flags first */
93
0
  if (super->flags & ~CRAMFS_SUPPORTED_FLAGS) {
94
0
    printf ("cramfs: unsupported filesystem features\n");
95
0
    return -1;
96
0
  }
97
98
  /* Check that the root inode is in a sane state */
99
0
  if (!S_ISDIR (CRAMFS_16 (super->root.mode))) {
100
0
    printf ("cramfs: root is not a directory\n");
101
0
    return -1;
102
0
  }
103
0
  root_offset = CRAMFS_GET_OFFSET (&(super->root)) << 2;
104
0
  if (root_offset == 0) {
105
0
    printf ("cramfs: empty filesystem");
106
0
  } else if (!(super->flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) &&
107
0
       ((root_offset != sizeof (struct cramfs_super)) &&
108
0
        (root_offset != 512 + sizeof (struct cramfs_super)))) {
109
0
    printf ("cramfs: bad root offset %lu\n", root_offset);
110
0
    return -1;
111
0
  }
112
113
0
  return 0;
114
0
}
115
116
static int cramfs_read_file(struct inode *inode, unsigned long offset,
117
          void *buf, size_t size)
118
0
{
119
0
  struct cramfs_inode_info *info = to_cramfs_inode_info(inode);
120
0
  struct cramfs_inode *cramfs_inode = &info->inode;
121
0
  struct fs_device *fsdev = container_of(inode->i_sb, struct fs_device,
122
0
                 sb);
123
0
  struct cramfs_priv *priv = fsdev->dev.priv;
124
0
  unsigned int blocknr;
125
0
  int outsize = 0;
126
0
  static char cramfs_read_buf[4096];
127
128
0
  while (size) {
129
0
    uint32_t base;
130
0
    int copy, ofs;
131
0
    ssize_t ret;
132
133
0
    blocknr = (offset + outsize) >> 12;
134
0
    ofs = (offset + outsize) % 4096;
135
0
    if (blocknr) {
136
0
      ret = cdev_read(priv->cdev, &base, 4,
137
0
          OFFSET(inode) + (blocknr - 1) * 4, 0);
138
0
      if (ret < 0)
139
0
        return ret;
140
0
      if (ret != 4)
141
0
        return -EIO;
142
0
    } else {
143
0
      base = (CRAMFS_GET_OFFSET(cramfs_inode) +
144
0
        (((CRAMFS_24 (cramfs_inode->size)) + 4095) >> 12)) << 2;
145
0
    }
146
147
0
    if (priv->curr_base < 0 || priv->curr_base != base) {
148
149
0
      ret = cdev_read(priv->cdev, cramfs_read_buf, 4096, base, 0);
150
0
      if (ret < 0)
151
0
        return ret;
152
153
      /* priv->buf won't hold the cached block anymore */
154
0
      priv->curr_base = -1;
155
0
      priv->curr_block_len = cramfs_uncompress_block(priv->buf, 4096,
156
0
          cramfs_read_buf, 4096);
157
0
      if (priv->curr_block_len <= 0)
158
0
        break;
159
160
0
      priv->curr_base = base;
161
0
    }
162
163
    /* only the last block of a file may be short */
164
0
    if (ofs >= priv->curr_block_len)
165
0
      break;
166
167
0
    copy = min_t(size_t, priv->curr_block_len - ofs, size);
168
169
0
    memcpy(buf, priv->buf + ofs, copy);
170
171
0
    outsize += copy;
172
0
    size -= copy;
173
0
    buf += copy;
174
0
  }
175
176
0
  return outsize;
177
0
}
178
179
static int cramfs_read(struct file *f, void *buf, size_t size)
180
0
{
181
0
  return cramfs_read_file(f->f_inode, f->f_pos, buf, size);
182
0
}
183
184
#if 0
185
static int cramfs_info (struct device *dev)
186
{
187
  if (cramfs_read_super (dev))
188
    return 0;
189
190
  printf ("size: 0x%x (%u)\n", super.size, super.size);
191
192
  if (super.flags != 0) {
193
    printf ("flags:\n");
194
    if (super.flags & CRAMFS_FLAG_FSID_VERSION_2)
195
      printf ("\tFSID version 2\n");
196
    if (super.flags & CRAMFS_FLAG_SORTED_DIRS)
197
      printf ("\tsorted dirs\n");
198
    if (super.flags & CRAMFS_FLAG_HOLES)
199
      printf ("\tholes\n");
200
    if (super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET)
201
      printf ("\tshifted root offset\n");
202
  }
203
204
  printf ("fsid:\n\tcrc: 0x%x\n\tedition: 0x%x\n",
205
    super.fsid.crc, super.fsid.edition);
206
  printf ("name: %16s\n", super.name);
207
208
  return 1;
209
}
210
#endif
211
212
static const struct file_operations cramfs_dir_operations;
213
static const struct inode_operations cramfs_dir_inode_operations;
214
static const struct inode_operations cramfs_symlink_inode_operations;
215
static const struct file_operations cramfs_file_operations;
216
217
static unsigned long cramino(const struct cramfs_inode *cino, unsigned int offset)
218
0
{
219
0
  if (!cino->offset)
220
0
    return offset + 1;
221
0
  if (!cino->size)
222
0
    return offset + 1;
223
224
  /*
225
   * The file mode test fixes buggy mkcramfs implementations where
226
   * cramfs_inode->offset is set to a non zero value for entries
227
   * which did not contain data, like devices node and fifos.
228
   */
229
0
  switch (cino->mode & S_IFMT) {
230
0
  case S_IFREG:
231
0
  case S_IFDIR:
232
0
  case S_IFLNK:
233
0
    return cino->offset << 2;
234
0
  default:
235
0
    break;
236
0
  }
237
0
  return offset + 1;
238
0
}
239
240
static struct inode *get_cramfs_inode(struct super_block *sb,
241
  const struct cramfs_inode *cramfs_inode, unsigned int offset)
242
0
{
243
0
  struct cramfs_inode_info *info;
244
0
  static struct timespec zerotime;
245
0
  struct inode *inode;
246
247
0
  inode = new_inode(sb);
248
249
0
  inode->i_ino = cramino(cramfs_inode, offset);
250
251
0
  info = to_cramfs_inode_info(inode);
252
253
0
  switch (cramfs_inode->mode & S_IFMT) {
254
0
  case S_IFREG:
255
0
    inode->i_fop = &cramfs_file_operations;
256
0
    break;
257
0
  case S_IFDIR:
258
0
    inode->i_op = &cramfs_dir_inode_operations;
259
0
    inode->i_fop = &cramfs_dir_operations;
260
0
    break;
261
0
  case S_IFLNK:
262
0
    inode->i_op = &cramfs_symlink_inode_operations;
263
0
    break;
264
0
  default:
265
0
    iput(inode);
266
0
    return NULL;
267
0
  }
268
269
0
  info->inode = *cramfs_inode;
270
271
0
  inode->i_mode = cramfs_inode->mode;
272
273
  /* if the lower 2 bits are zero, the inode contains data */
274
0
  if (!(inode->i_ino & 3)) {
275
0
    inode->i_size = cramfs_inode->size;
276
0
    inode->i_blocks = (cramfs_inode->size - 1) / 512 + 1;
277
0
  }
278
279
  /* Struct copy intentional */
280
0
  inode->i_mtime = inode->i_atime = inode->i_ctime = zerotime;
281
  /* inode->i_nlink is left 1 - arguably wrong for directories,
282
     but it's the best we can do without reading the directory
283
     contents.  1 yields the right result in GNU find, even
284
     without -noleaf option. */
285
286
0
  return inode;
287
0
}
288
289
static struct dentry *cramfs_lookup(struct inode *dir, struct dentry *dentry,
290
            unsigned int flags)
291
0
{
292
0
  struct cramfs_inode *de;
293
0
  unsigned int offset = 0;
294
0
  struct inode *inode = NULL;
295
0
  struct fs_device *fsdev = container_of(dir->i_sb, struct fs_device,
296
0
                 sb);
297
0
  struct cramfs_priv *priv = fsdev->dev.priv;
298
299
0
  de = xmalloc(sizeof(*de) + CRAMFS_MAXPATHLEN);
300
301
0
  while (offset < dir->i_size) {
302
0
    char *name;
303
0
    int namelen, retval;
304
0
    int dir_off = OFFSET(dir) + offset;
305
0
    ssize_t ret;
306
307
0
    ret = cdev_read(priv->cdev, de, sizeof(*de) + CRAMFS_MAXPATHLEN, dir_off, 0);
308
0
    if (ret < 0) {
309
0
      inode = ERR_PTR(ret);
310
0
      goto out;
311
0
    }
312
313
0
    name = (char *)(de + 1);
314
315
0
    namelen = de->namelen << 2;
316
0
    if (ret < (ssize_t)(sizeof(*de) + namelen)) {
317
0
      inode = ERR_PTR(-EIO);
318
0
      goto out;
319
0
    }
320
0
    offset += sizeof(*de) + namelen;
321
322
    /* Quick check that the name is roughly the right length */
323
0
    if (((dentry->d_name.len + 3) & ~3) != namelen)
324
0
      continue;
325
326
0
    for (;;) {
327
0
      if (!namelen) {
328
0
        inode = ERR_PTR(-EIO);
329
0
        goto out;
330
0
      }
331
0
      if (name[namelen-1])
332
0
        break;
333
0
      namelen--;
334
0
    }
335
0
    if (namelen != dentry->d_name.len)
336
0
      continue;
337
0
    retval = memcmp(dentry->d_name.name, name, namelen);
338
0
    if (retval > 0)
339
0
      continue;
340
0
    if (!retval) {
341
0
      inode = get_cramfs_inode(dir->i_sb, de, dir_off);
342
0
      break;
343
0
    }
344
0
  }
345
0
out:
346
0
  free(de);
347
348
0
  if (IS_ERR(inode))
349
0
    return ERR_CAST(inode);
350
0
  d_add(dentry, inode);
351
352
0
  return NULL;
353
0
}
354
355
static struct inode *cramfs_alloc_inode(struct super_block *sb)
356
0
{
357
0
  struct cramfs_inode_info *info;
358
359
0
  info = xzalloc(sizeof(*info));
360
361
0
  return &info->i_inode;
362
0
}
363
364
static void cramfs_destroy_inode(struct inode *inode)
365
0
{
366
0
  struct cramfs_inode_info *info;
367
368
0
  info = to_cramfs_inode_info(inode);
369
370
0
  free(info);
371
0
}
372
373
static int cramfs_iterate(struct file *file, struct dir_context *ctx)
374
0
{
375
0
  struct dentry *dentry = file->f_path.dentry;
376
0
  struct inode *dir = d_inode(dentry);
377
0
  struct fs_device *fsdev = container_of(dir->i_sb, struct fs_device,
378
0
                 sb);
379
0
  struct cramfs_priv *priv = fsdev->dev.priv;
380
0
  char *buf;
381
0
  unsigned int offset;
382
0
  struct cramfs_inode *de;
383
0
  int ret;
384
385
  /* Offset within the thing. */
386
0
  if (ctx->pos >= dir->i_size)
387
0
    return 0;
388
0
  offset = ctx->pos;
389
  /* Directory entries are always 4-byte aligned */
390
0
  if (offset & 3)
391
0
    return -EINVAL;
392
393
0
  buf = xmalloc(CRAMFS_MAXPATHLEN);
394
0
  de = xmalloc(sizeof(*de) + CRAMFS_MAXPATHLEN);
395
396
0
  while (offset < dir->i_size) {
397
0
    unsigned long nextoffset;
398
0
    char *name;
399
0
    ino_t ino;
400
0
    umode_t mode;
401
0
    int namelen;
402
0
    ssize_t len;
403
404
0
    len = cdev_read(priv->cdev, de, sizeof(*de) + CRAMFS_MAXPATHLEN,
405
0
        OFFSET(dir) + offset, 0);
406
0
    if (len < 0) {
407
0
      ret = len;
408
0
      goto out;
409
0
    }
410
0
    name = (char *)(de + 1);
411
412
    /*
413
     * Namelengths on disk are shifted by two
414
     * and the name padded out to 4-byte boundaries
415
     * with zeroes.
416
     */
417
0
    namelen = de->namelen << 2;
418
0
    if (len < (ssize_t)(sizeof(*de) + namelen)) {
419
0
      ret = -EIO;
420
0
      goto out;
421
0
    }
422
0
    memcpy(buf, name, namelen);
423
0
    ino = cramino(de, OFFSET(dir) + offset);
424
0
    mode = de->mode;
425
426
0
    nextoffset = offset + sizeof(*de) + namelen;
427
0
    for (;;) {
428
0
      if (!namelen) {
429
0
        ret = -EIO;
430
0
        goto out;
431
0
      }
432
0
      if (buf[namelen - 1])
433
0
        break;
434
0
      namelen--;
435
0
    }
436
437
0
    dir_emit(ctx, buf, namelen, ino, mode >> 12);
438
439
0
    ctx->pos = offset = nextoffset;
440
0
  }
441
0
  ret = 0;
442
0
out:
443
0
  kfree(buf);
444
0
  free(de);
445
0
  return ret;
446
0
}
447
448
static const struct file_operations cramfs_dir_operations = {
449
  .iterate = cramfs_iterate,
450
};
451
452
static const struct file_operations cramfs_file_operations = {
453
  .read = cramfs_read,
454
};
455
456
static const struct inode_operations cramfs_dir_inode_operations =
457
{
458
  .lookup = cramfs_lookup,
459
};
460
461
static const char *cramfs_get_link(struct dentry *dentry, struct inode *inode)
462
0
{
463
0
  int ret;
464
465
0
  inode->i_link = xzalloc(size_add(inode->i_size, 1));
466
467
0
  ret = cramfs_read_file(inode, 0, inode->i_link, inode->i_size);
468
0
  if (ret != inode->i_size) {
469
0
    free(inode->i_link);
470
0
    inode->i_link = NULL;
471
0
    return ERR_PTR(-EIO);
472
0
  }
473
474
0
  return inode->i_link;
475
0
}
476
477
static const struct inode_operations cramfs_symlink_inode_operations =
478
{
479
  .get_link = cramfs_get_link,
480
};
481
482
static const struct super_operations cramfs_ops = {
483
  .alloc_inode = cramfs_alloc_inode,
484
  .destroy_inode = cramfs_destroy_inode,
485
};
486
487
static int cramfs_probe(struct device *dev)
488
0
{
489
0
  struct fs_device *fsdev;
490
0
  struct cramfs_priv *priv;
491
0
  int ret;
492
0
  struct super_block *sb;
493
0
  struct inode *root;
494
495
0
  fsdev = dev_to_fs_device(dev);
496
0
  sb = &fsdev->sb;
497
498
0
  priv = xmalloc(sizeof(struct cramfs_priv));
499
0
  dev->priv = priv;
500
501
0
  ret = fsdev_open_cdev(fsdev);
502
0
  if (ret) {
503
0
    dev_err(dev, "open cdev failed: %d\n", ret);
504
0
    goto err_out;
505
0
  }
506
507
0
  priv->cdev = fsdev->cdev;
508
509
0
  if (cramfs_read_super(priv)) {
510
0
    dev_info(dev, "no valid cramfs found\n");
511
0
    ret =  -EINVAL;
512
0
    goto err_out;
513
0
  }
514
515
0
  priv->curr_base = -1;
516
517
0
  sb->s_op = &cramfs_ops;
518
519
0
  root = get_cramfs_inode(sb, &priv->super.root, 0);
520
0
  if (!root) {
521
0
    ret = -EINVAL;
522
0
    goto err_out;
523
0
  }
524
0
  sb->s_root = d_make_root(root);
525
526
0
  cramfs_uncompress_init();
527
528
0
  return 0;
529
530
0
err_out:
531
0
  free(priv);
532
533
0
  return ret;
534
0
}
535
536
static void cramfs_remove(struct device *dev)
537
0
{
538
0
  struct cramfs_priv *priv = dev->priv;
539
540
0
  cramfs_uncompress_exit();
541
0
  free(priv);
542
0
}
543
544
static struct fs_driver cramfs_driver = {
545
  .drv = {
546
    .probe = cramfs_probe,
547
    .remove = cramfs_remove,
548
    .name = "cramfs",
549
  }
550
};
551
552
static int cramfs_init(void)
553
32
{
554
32
  return register_fs_driver(&cramfs_driver);
555
32
}
556
557
device_initcall(cramfs_init);