Skip to content

Commit 8d94a46

Browse files
misaka36dhewg
andauthored
fritz-tools: sync with upstream (coolsnowwolf#10574)
* fritz-tools: fritz_tffs_nand: exclude oob code when disabled Skip unnecessary stuff if checking the oob data is disabled. Signed-off-by: Andre Heider <[email protected]> * fritz-tools: fritz_tffs_nand: get rid of struct tffs_sectors This doesn't help and "[0]" gets in the way of bounds checks. Signed-off-by: Andre Heider <[email protected]> * fritz-tools: fritz_tffs_nand: cache already read sector ids This speeds up the tool significantly, especially when using the "-a" argument. Signed-off-by: Andre Heider <[email protected]> Signed-off-by: Andre Heider <[email protected]> Co-authored-by: Andre Heider <[email protected]>
1 parent d28b089 commit 8d94a46

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

package/utils/fritz-tools/src/fritz_tffs_nand_read.c

+34-19
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,18 @@ static uint8_t readbuf[TFFS_SECTOR_SIZE];
7373
static uint8_t oobbuf[TFFS_SECTOR_OOB_SIZE];
7474
static uint32_t blocksize;
7575
static int mtdfd;
76-
struct tffs_sectors *sectors;
77-
78-
struct tffs_sectors {
79-
uint32_t num_sectors;
80-
uint8_t sectors[0];
81-
};
76+
static uint32_t num_sectors;
77+
static uint8_t *sectors;
78+
static uint32_t *sector_ids;
8279

8380
static inline void sector_mark_bad(int num)
8481
{
85-
sectors->sectors[num / 8] &= ~(0x80 >> (num % 8));
82+
sectors[num / 8] &= ~(0x80 >> (num % 8));
8683
};
8784

8885
static inline uint8_t sector_get_good(int num)
8986
{
90-
return sectors->sectors[num / 8] & 0x80 >> (num % 8);
87+
return sectors[num / 8] & 0x80 >> (num % 8);
9188
};
9289

9390
struct tffs_entry_segment {
@@ -139,6 +136,8 @@ static int read_sector(off_t pos)
139136
return -1;
140137
}
141138

139+
sector_ids[pos / TFFS_SECTOR_SIZE] = read_uint32(readbuf, 0x00);
140+
142141
return 0;
143142
}
144143

@@ -176,25 +175,39 @@ static int find_entry(uint32_t id, struct tffs_entry *entry)
176175

177176
off_t pos = 0;
178177
uint8_t block_end = 0;
179-
for (uint32_t sector = 0; sector < sectors->num_sectors; sector++, pos += TFFS_SECTOR_SIZE) {
178+
for (uint32_t sector = 0; sector < num_sectors; sector++, pos += TFFS_SECTOR_SIZE) {
180179
if (block_end) {
181180
if (pos % blocksize == 0) {
182181
block_end = 0;
183182
}
184183
} else if (sector_get_good(sector)) {
184+
if (sector_ids[sector]) {
185+
if (sector_ids[sector] == TFFS_ID_END) {
186+
/* no more entries in this block */
187+
block_end = 1;
188+
continue;
189+
}
190+
191+
if (sector_ids[sector] != id)
192+
continue;
193+
}
194+
185195
if (read_sectoroob(pos) || read_sector(pos)) {
186196
fprintf(stderr, "ERROR: sector isn't readable, but has been previously!\n");
187197
exit(EXIT_FAILURE);
188198
}
189-
uint32_t oob_id = read_uint32(oobbuf, 0x02);
190-
uint32_t oob_len = read_uint32(oobbuf, 0x06);
191-
uint32_t oob_rev = read_uint32(oobbuf, 0x0a);
192199
uint32_t read_id = read_uint32(readbuf, 0x00);
193200
uint32_t read_len = read_uint32(readbuf, 0x04);
194201
uint32_t read_rev = read_uint32(readbuf, 0x0c);
195-
if (read_oob_sector_health && (oob_id != read_id || oob_len != read_len || oob_rev != read_rev)) {
196-
fprintf(stderr, "Warning: sector has inconsistent metadata\n");
197-
continue;
202+
if (read_oob_sector_health) {
203+
uint32_t oob_id = read_uint32(oobbuf, 0x02);
204+
uint32_t oob_len = read_uint32(oobbuf, 0x06);
205+
uint32_t oob_rev = read_uint32(oobbuf, 0x0a);
206+
207+
if (oob_id != read_id || oob_len != read_len || oob_rev != read_rev) {
208+
fprintf(stderr, "Warning: sector has inconsistent metadata\n");
209+
continue;
210+
}
198211
}
199212
if (read_id == TFFS_ID_END) {
200213
/* no more entries in this block */
@@ -414,13 +427,14 @@ static int scan_mtd(void)
414427

415428
blocksize = info.erasesize;
416429

417-
sectors = malloc(sizeof(*sectors) + (info.size / TFFS_SECTOR_SIZE + 7) / 8);
418-
if (sectors == NULL) {
430+
num_sectors = info.size / TFFS_SECTOR_SIZE;
431+
sectors = malloc((num_sectors + 7) / 8);
432+
sector_ids = calloc(num_sectors, sizeof(uint32_t));
433+
if (!sectors || !sector_ids) {
419434
fprintf(stderr, "ERROR: memory allocation failed!\n");
420435
exit(EXIT_FAILURE);
421436
}
422-
sectors->num_sectors = info.size / TFFS_SECTOR_SIZE;
423-
memset(sectors->sectors, 0xff, (info.size / TFFS_SECTOR_SIZE + 7) / 8);
437+
memset(sectors, 0xff, (num_sectors + 7) / 8);
424438

425439
uint32_t sector = 0, valid_blocks = 0;
426440
uint8_t block_ok = 0;
@@ -564,6 +578,7 @@ int main(int argc, char *argv[])
564578
out_free_entry:
565579
free(name_table.val);
566580
out_free_sectors:
581+
free(sector_ids);
567582
free(sectors);
568583
out_close:
569584
close(mtdfd);

0 commit comments

Comments
 (0)