1 // SPDX-License-Identifier: GPL-2.0-or-later
7 read_uint_from_file(char *dirname
, char *filename
, unsigned int *i
)
13 snprintf(fname
, sizeof(fname
), "%s/%s", dirname
, filename
);
15 f
= fopen(fname
, "r");
19 if (fscanf(f
, "%u", i
) == 1)
27 *read_string_from_file(const char *dirname
, const char *filename
, char *buf
, size_t bufsz
)
33 snprintf(fname
, sizeof(fname
), "%s/%s", dirname
, filename
);
35 f
= fopen(fname
, "r");
39 if (fgets(buf
, bufsz
, f
) == NULL
) {
46 /* make sure the string is \0 terminated */
47 buf
[bufsz
- 1] = '\0';
49 /* remove trailing whitespace */
51 while (i
> 0 && buf
[i
] <= ' ')
57 int block_file_identify(FILE *f
, uint64_t offset
)
62 if (fseeko(f
, offset
, SEEK_SET
) < 0)
65 n
= fread(&magic
, sizeof(magic
), 1, f
);
66 if (magic
== cpu_to_le32(0x88b1f))
69 if (magic
== cpu_to_be32(0xdeadc0de))
72 if (fseeko(f
, offset
+ 0x400, SEEK_SET
) < 0)
75 n
= fread(&magic
, sizeof(magic
), 1, f
);
79 if (magic
== cpu_to_le32(0xF2F52010))
83 if (fseeko(f
, offset
+ 0x438, SEEK_SET
) < 0)
86 n
= fread(&magic
, sizeof(magic
), 1, f
);
90 if ((le32_to_cpu(magic
) & 0xffff) == 0xef53)
96 static bool use_f2fs(struct volume
*v
, uint64_t offset
, const char *bdev
)
102 fd
= open(bdev
, O_RDONLY
);
106 if (ioctl(fd
, BLKGETSIZE64
, &size
) == 0)
107 ret
= size
- offset
> F2FS_MINSIZE
;
114 int block_volume_format(struct volume
*v
, uint64_t offset
, const char *bdev
)
118 unsigned int skip_blocks
= 0;
123 switch (volume_identify(v
)) {
126 fd
= open(v
->blk
, O_RDONLY
);
132 if (lseek(fd
, (skip_blocks
+ 1) * 512, SEEK_SET
) == (off_t
) -1) {
136 sz
= read(fd
, &deadc0de
, sizeof(deadc0de
));
137 if (sz
!= sizeof(deadc0de
)) {
141 } while(++skip_blocks
<= 512 &&
142 (deadc0de
== cpu_to_be32(0xdeadc0de) || deadc0de
== 0xffffffff));
148 /* only try extracting in case gzip header is present */
149 if (deadc0de
!= cpu_to_le32(0x88b1f))
154 snprintf(str
, sizeof(str
),
155 "dd if=%s bs=512 skip=%u 2>/dev/null | gzip -cd > /tmp/sysupgrade.tar 2>/dev/null",
156 v
->blk
, skip_blocks
);
159 ULOG_ERR("failed extracting config backup from %s\n", v
->blk
);
165 ULOG_INFO("overlay filesystem in %s has not been formatted yet\n", v
->blk
);
166 if (use_f2fs(v
, offset
, bdev
))
167 snprintf(str
, sizeof(str
), "mkfs.f2fs -q -l rootfs_data %s", v
->blk
);
169 snprintf(str
, sizeof(str
), "mkfs.ext4 -q -L rootfs_data %s", v
->blk
);