summaryrefslogtreecommitdiffstats
path: root/utils/nwztools/database/nvp/nvptool.cpp
blob: 8c09d1bea093d23404378544c6265d6855f2463b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 Amaury Pouly
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <cstdio>
#include <stdint.h>
#include <cstdlib>
#include <cstring>
#include <getopt.h>
#include <cstdarg>
#include <string>
#include <fstream>
#include <elf.h>

bool g_verbose = false;
bool g_unsafe = false;

uint8_t *read_file(const std::string& path, size_t& size)
{
    std::ifstream fin(path.c_str(), std::ios::binary);
    if(!fin)
    {
        printf("Error: cannot open '%s'\n", path.c_str());
        return 0;
    }
    fin.seekg(0, std::ios::end);
    size = fin.tellg();
    fin.seekg(0, std::ios::beg);
    uint8_t *buf = new uint8_t[size];
    fin.read((char *)buf, size);
    return buf;
}

bool write_file(const std::string& path, uint8_t *buf, size_t size)
{
    std::ofstream fout(path.c_str(), std::ios::binary);
    if(!fout)
    {
        printf("Error: cannot open '%s'\n", path.c_str());
        return false;
    }
    fout.write((char *)buf, size);
    fout.close();
    return true;
}

/* ELF code */
uint8_t *g_elf_buf;
size_t g_elf_size;
Elf32_Shdr *g_elf_symtab;
Elf32_Shdr *g_elf_symtab_strtab;
Elf32_Shdr *g_elf_shstrtab;

Elf32_Ehdr *elf_ehdr()
{
    return (Elf32_Ehdr *)g_elf_buf;
}

#define NTH_SHDR_OFF(n) \
    (elf_ehdr()->e_shoff + elf_ehdr()->e_shentsize * (n))

Elf32_Shdr *elf_shdr(size_t index)
{
    if(index >= elf_ehdr()->e_shnum)
    {
        printf("Warning: section index is out of bounds\n");
        return nullptr;
    }
    return (Elf32_Shdr *)(g_elf_buf + NTH_SHDR_OFF(index));
}

size_t elf_shnum()
{
    return elf_ehdr()->e_shnum;
}

const char *elf_get_str(Elf32_Shdr *strtab, Elf32_Word index)
{
    /* sanity checks */
    if(strtab->sh_type != SHT_STRTAB)
    {
        printf("Warning: string access to a non-string-table section\n");
        return nullptr;
    }
    if(strtab->sh_offset + strtab->sh_size > g_elf_size)
    {
        printf("Warning: string table section does not fit in the file\n");
        return nullptr;
    }
    if(index >= strtab->sh_size)
    {
        printf("Warning: string access to string table is out of bounds\n");
        return nullptr;
    }
    char *buf = (char *)(g_elf_buf + strtab->sh_offset);
    if(buf[strtab->sh_size - 1] != 0)
    {
        printf("Warning: string table is not zero terminated\n");
        return nullptr;
    }
    return buf + index;
}

const char *elf_get_section_name(size_t index)
{
    Elf32_Shdr *shdr = elf_shdr(index);
    return shdr ? elf_get_str(g_elf_shstrtab, shdr->sh_name) : nullptr;
}

const char *elf_get_symbol_name(Elf32_Sym *sym)
{
    if(ELF32_ST_TYPE(sym->st_info) == STT_SECTION)
        return elf_get_section_name(sym->st_shndx);
    else
        return elf_get_str(g_elf_symtab_strtab, sym->st_name);
}

Elf32_Sym *elf_get_symbol_by_name(const char *name)
{
    Elf32_Sym *sym = (Elf32_Sym *)(g_elf_buf + g_elf_symtab->sh_offset);
    size_t nr_syms = g_elf_symtab->sh_size / sizeof(Elf32_Sym);
    for(size_t i = 0; i < nr_syms; i++)
    {
        const char *s = elf_get_symbol_name(&sym[i]);
        if(s != nullptr && strcmp(name, s) == 0)
            return &sym[i];
    }
    return nullptr;
}

Elf32_Sym *elf_get_symbol_by_address(size_t shndx, Elf32_Word address)
{
    Elf32_Sym *sym = (Elf32_Sym *)(g_elf_buf + g_elf_symtab->sh_offset);
    size_t nr_syms = g_elf_symtab->sh_size / sizeof(Elf32_Sym);
    for(size_t i = 0; i < nr_syms; i++)
    {
        if(sym[i].st_shndx == shndx && sym[i].st_value == address)
            return &sym[i];
    }
    return nullptr;
}

Elf32_Sym *elf_get_symbol_by_index(size_t index)
{
    Elf32_Sym *sym = (Elf32_Sym *)(g_elf_buf + g_elf_symtab->sh_offset);
    size_t nr_syms = g_elf_symtab->sh_size / sizeof(Elf32_Sym);
    if(index >= nr_syms)
        return nullptr;
    return &sym[index];
}

void *elf_get_section_ptr(size_t shndx, Elf32_Word address, size_t size)
{
    Elf32_Shdr *shdr = elf_shdr(shndx);
    if(shdr == nullptr)
        return nullptr;
    if(address + size > shdr->sh_size)
        return nullptr;
    if(shdr->sh_offset + shdr->sh_size > g_elf_size)
        return nullptr;
    return g_elf_buf + shdr->sh_offset + address;
}

/* make sure the string has a final zero in the section, optionally check characters
 * are printable */
const char *elf_get_string_ptr_safe(size_t shndx, Elf32_Word offset, bool want_print = true)
{
    Elf32_Shdr *shdr = elf_shdr(shndx);
    if(shdr == nullptr)
        return nullptr;
    /* address must be in the section */
    if(offset >= shdr->sh_size)
        return nullptr;
    /* determine maximum size */
    size_t max_sz = shdr->sh_size - offset;
    const char *ptr = (const char *)(g_elf_buf + shdr->sh_offset + offset);
    for(size_t i = 0; i < max_sz; i++)
    {
        if(ptr[i] == 0) /* found final 0, everything is fine */
            return ptr;
        if(want_print && !isprint(ptr[i]))
            return nullptr;
    }
    return nullptr;
}

size_t elf_find_reloc_section(size_t shndx)
{
    /* find the relocation section */
    for(size_t i = 0; i < elf_ehdr()->e_shnum; i++)
    {
        Elf32_Shdr *shdr = elf_shdr(i);
        if(shdr->sh_type != SHT_REL && shdr->sh_type != SHT_RELA)
            continue;
        if(shdr->sh_info != shndx)
            continue;
        return i;
    }
    return 0;
}

void *elf_get_symbol_ptr(Elf32_Sym *sym, size_t size)
{
    /* NOTE: also works for STT_SECTION since offset will be 0 */
    return elf_get_section_ptr(sym->st_shndx, sym->st_value, size);
}

/* take the position of a 32-bit address in the section and apply relocation if
 * any */
void *elf_reloc_addr32(size_t shndx, Elf32_Word offset)
{
    /* read value */
    uint32_t *val = (uint32_t *)elf_get_section_ptr(shndx, offset, 4);
    if(val == nullptr)
        return 0; /* invalid */
    /* find reloc section if any */
    size_t relshndx = elf_find_reloc_section(shndx);
    if(relshndx == 0)
        return g_elf_buf + *val; /* no relocation applies */
    Elf32_Shdr *shdr = elf_shdr(relshndx);
    /* find relocation that applies */
    if(shdr->sh_type == SHT_RELA)
    {
        printf("Warning: unsupported RELA relocation type\n");
        return 0;
    }
    Elf32_Rel *rel = (Elf32_Rel *)elf_get_section_ptr(relshndx, 0, shdr->sh_size);
    if(rel == nullptr)
    {
        printf("Warning: invalid relocation section\n");
        return 0;
    }
    size_t sym_count = shdr->sh_size / sizeof(Elf32_Rel);
    for(size_t i = 0; i < sym_count; i++)
    {
        /* for relocatable files, r_offset is the offset in the section */
        if(rel[i].r_offset != offset)
            continue;
        /* find symbol, ignore shdr->sh_link and assume it is g_elf_symtab
         * since the file should have only one symbol table anyway */
        Elf32_Sym *sym = elf_get_symbol_by_index(ELF32_R_SYM(rel[i].r_info));
        /* found it! */
        if(g_verbose)
        {
            printf("[section %zu (%s) offset %#x reloc val %#x type %d sym %d (%s)]\n",
                shndx, elf_get_section_name(shndx), offset, *val,
                ELF32_R_TYPE(rel[i].r_info), ELF32_R_SYM(rel[i].r_info),
                sym ? elf_get_symbol_name(sym) : "<undef>");
        }
        /* apply reloc */
        if(ELF32_R_TYPE(rel[i].r_info) == R_ARM_ABS32)
        {
            if(sym == nullptr)
            {
                printf("Warning: R_ARM_ABS32 reloc with invalid symbol reference\n");
                return 0;
            }
            return *val + (uint8_t *)elf_get_symbol_ptr(sym, 0);
        }
        else
        {
            printf("Warning: unsupported relocation type %d\n", ELF32_R_TYPE(rel[i].r_info));
            return 0;
        }
    }
    /* no reloc applies */
    if(g_verbose)
    {
        printf("[section %zu (%s) offset %#x no reloc found]\n", shndx,
            elf_get_section_name(shndx), offset);
    }
    return g_elf_buf + *val; /* no relocation applies */
}

size_t elf_map_virt_addr(uint32_t address, Elf32_Word& out_off)
{
    /* for relocatable file, this is trivial */
    for(size_t i = 0; i < elf_ehdr()->e_shnum; i++)
    {
        Elf32_Shdr *shdr = elf_shdr(i);
        if(shdr->sh_offset <= address && address < shdr->sh_offset + shdr->sh_size)
        {
            out_off = address - shdr->sh_offset;
            if(g_verbose)
            {
                printf("[map %#x to section %zi (%s) at %#x]\n", address, i,
                    elf_get_section_name(i), out_off);
            }
            return i;
        }
    }
    return 0; /* section 0 is always invalid */
}

size_t elf_map_ptr(void *ptr, Elf32_Word& out_off)
{
    uint32_t addr = (uint32_t)((uint8_t *)ptr - g_elf_buf);
    return elf_map_virt_addr(addr, out_off);
}

/* same as elf_reloc_addr32 but find section automatically from pointer */
void *elf_reloc_addr32_ptr(uint32_t *val)
{
    Elf32_Word off;
    size_t sec = elf_map_ptr((void *)val, off);
    /* if it does not belong to any section, don't do anything */
    if(sec == 0)
    {
        printf("Warning: reloc addr pointer not in any section\n");
        return g_elf_buf + *val;
    }
    return elf_reloc_addr32(sec, off);
}

Elf32_Sym *elf_get_symbol_by_ptr(void *ptr)
{
    Elf32_Word off;
    size_t sec = elf_map_ptr(ptr, off);
    return sec ? elf_get_symbol_by_address(sec, off) : nullptr;
}

/* check if a string is safe */
bool elf_is_str_ptr_safe(const char *str)
{
    Elf32_Word name_off;
    /* find the section it belongs to */
    size_t name_shndx = elf_map_ptr((void *)str, name_off);
    if(name_shndx == 0)
        return false;
    /* check the string fit in the section */
    return elf_get_string_ptr_safe(name_shndx, name_off) != nullptr;
}

bool elf_is_ptr_safe(void *ptr, size_t sz)
{
    Elf32_Word ptr_off;
    /* find the section it belongs to */
    size_t ptr_shndx = elf_map_ptr((void *)ptr, ptr_off);
    if(ptr_shndx == 0)
        return false;
    /* check the string fit in the section */
    return elf_get_section_ptr(ptr_shndx, ptr_off, sz) != nullptr;
}

bool elf_init()
{
    if(g_elf_size < sizeof(Elf32_Ehdr))
    {
        printf("Invalid ELF file: too small\n");
        return false;
    }
    Elf32_Ehdr *ehdr = elf_ehdr();
    if(ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
            ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
            ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
            ehdr->e_ident[EI_MAG3] != ELFMAG3)
    {
        printf("Invalid ELF file: invalid ident\n");
        return false;
    }
    /* we only support relocatable files */
    if(ehdr->e_type != ET_REL)
    {
        printf("Unsupported ELF file: this is not a relocatable file\n");
        return false;
    }
    if(ehdr->e_ident[EI_CLASS] != ELFCLASS32 || ehdr->e_machine != EM_ARM)
    {
        printf("Unsupported ELF file: this is not a 32-bit ARM ELF file\n");
        return false;
    }
    /* go through sections */
    if(ehdr->e_shoff == 0)
    {
        printf("Invalid ELF file: no sections\n");
        return false;
    }
    if(ehdr->e_shentsize < sizeof(Elf32_Shdr))
    {
        printf("Invalid ELF file: section entry size too small\n");
        return false;
    }
    if(NTH_SHDR_OFF(ehdr->e_shnum) > g_elf_size)
    {
        printf("Invalid ELF file: sections header does not fit in the file\n");
        return false;
    }
    for(size_t i = 0; i < ehdr->e_shnum; i++)
    {
        Elf32_Shdr *shdr = (Elf32_Shdr *)(g_elf_buf + NTH_SHDR_OFF(i));
        if(shdr->sh_type == SHT_SYMTAB)
            g_elf_symtab = shdr;
    }
    /* handle symbol table */
    if(g_elf_symtab)
    {
        if(g_elf_symtab->sh_offset + g_elf_symtab->sh_size > g_elf_size)
        {
            printf("Invalid ELF file: symtab does not file in the file\n");
            return false;
        }
        g_elf_symtab_strtab = elf_shdr(g_elf_symtab->sh_link);
        if(g_elf_symtab_strtab == nullptr)
        {
            printf("Invalid ELF file: symtab's strtab is not valid\n");
        }
        if(g_elf_symtab_strtab->sh_type != SHT_STRTAB)
        {
            printf("Invalid ELF file: symtab's strtab is not a string table\n");
            return false;
        }
    }
    /* handle section string table */
    if(ehdr->e_shstrndx != SHN_UNDEF)
    {
        g_elf_shstrtab = elf_shdr(ehdr->e_shstrndx);
        if(g_elf_shstrtab == nullptr)
        {
            printf("Invalid ELF file: section string table is invalid\n");
            return false;
        }
    }

    return true;
}

/* main code */

void usage()
{
    printf("usage: nvptool [options] inputs...\n");
    printf("options:\n");
    printf("  -h/--help        Display help\n");
    printf("  -x/--extract     Extract nvp map from icx_nvp_emmc.ko\n");
    printf("  -o/--output      Set output file\n");
    printf("  -v/--verbose     Enable debug output\n");
    printf("  -u/--unsafe      Perform potentially unsafe operations\n");
    exit(1);
}

struct zone_info_v1_t
{
    uint32_t node;
    uint32_t start;
    uint32_t count;
    uint32_t size;
    uint32_t semaphore[4]; /* a 16-byte structure, useless for us */
    uint32_t name; /* pointer to string */
} __attribute__((packed));

struct zone_info_v2_t
{
    uint32_t node;
    uint32_t start;
    uint32_t count;
    uint32_t size;
    uint32_t semaphore[3]; /* a 12-byte structure, useless for us */
    uint32_t name; /* pointer to string */
} __attribute__((packed));

struct area_info_v1_t
{
    uint32_t type; /* 1 = large, 2 = small */
    uint32_t zoneinfo; /* pointer to zone_info_t[] */
    uint32_t zonecount;
    uint32_t semaphore[4]; /* a 16-byte structure, useless for us */
    uint32_t name; /* pointer to string */
} __attribute__((packed));

struct area_info_v2_t
{
    uint32_t type; /* 1 = large, 2 = small */
    uint32_t zoneinfo; /* pointer to zone_info_t[] */
    uint32_t zonecount;
    uint32_t semaphore[3]; /* a 16-byte structure, useless for us */
    uint32_t name; /* pointer to string */
} __attribute__((packed));

int guess_version(void *area_info_ptr)
{
    /* the "semaphore" part is always filled with zeroes, so simply check if there
     * are 3 or 4 of them */
    area_info_v1_t *ai_v1 = (area_info_v1_t *)area_info_ptr;
    if(ai_v1->semaphore[3] == 0)
        return 1; /* v1: semaphore has 4 fields */
    else
        return 2; /* v2: semaphore has 3 fields */
}

int do_extract(const char *output, int argc, char **argv)
{
    if(argc != 1)
    {
        printf("You need to specify exactly one input file to extract from.\n");
        return 3;
    }
    FILE *fout = NULL;
    if(output)
    {
        fout = fopen(output, "w");
        if(fout == NULL)
        {
            printf("Cannot open output file '%s'\n", output);
            return 4;
        }
    }
    /* read elf file */
    g_elf_buf = read_file(argv[0], g_elf_size);
    if(g_elf_buf == nullptr)
    {
        printf("Cannot open input file '%s'\n", argv[0]);
        return 1;
    }
    if(!elf_init())
    {
        printf("This is not a valid ELF file\n");
        return 1;
    }
    if(g_elf_symtab == nullptr)
    {
        printf("This ELF file does not have a symbol table\n");
        return 1;
    }
    /* look for symbol 'AreaInfo' */
    Elf32_Sym *sym_AreaInfo = elf_get_symbol_by_name("AreaInfo");
    if(sym_AreaInfo == nullptr)
    {
        printf("Cannot find symbol 'AreaInfo'\n");
        return 1;
    }
    printf("AreaInfo:\n");
    if(g_verbose)
    {
        printf("[%u bytes at address %#x in section %u (%s)]\n",
            (unsigned)sym_AreaInfo->st_size, (unsigned)sym_AreaInfo->st_value,
            (unsigned)sym_AreaInfo->st_shndx, elf_get_section_name(sym_AreaInfo->st_shndx));
    }
    /* guess version */
    int ver = guess_version(elf_get_symbol_ptr(sym_AreaInfo, sizeof(area_info_v1_t)));
    if(g_verbose)
        printf("[guessed version: %d]\n", ver);
    size_t sizeof_area_info = (ver == 1) ? sizeof(area_info_v1_t) : sizeof(area_info_v2_t);
    size_t sizeof_zone_info = (ver == 1) ? sizeof(zone_info_v1_t) : sizeof(zone_info_v2_t);
    /* sanity check AreaInfo */
    size_t area_count = sym_AreaInfo->st_size / sizeof_area_info;
    if(!g_unsafe && (sym_AreaInfo->st_size % sizeof_area_info) != 0)
    {
        printf("AreaInfo size (%u) is a not a multiple of area_info_t size (%zu).\n",
            (unsigned)sym_AreaInfo->st_size, sizeof_area_info);
        printf("Use unsafe option to override this check\n");
        return 1;
    }
    area_info_v1_t *AreaInfo_v1 = (area_info_v1_t *)elf_get_symbol_ptr(sym_AreaInfo,
        sym_AreaInfo->st_size);
    area_info_v2_t *AreaInfo_v2 = (area_info_v2_t *)AreaInfo_v1;
    if(AreaInfo_v1 == nullptr)
    {
        printf("Symbol does not point to a valid address\n");
        return 1;
    }
    for(size_t i = 0; i < area_count; i++)
    {
        uint32_t type;
        uint32_t *zoneinfo_ptr;
        uint32_t zonecount;
        uint32_t *name_ptr;

        if(ver == 1)
        {
            type = AreaInfo_v1[i].type;
            zoneinfo_ptr = &AreaInfo_v1[i].zoneinfo;
            zonecount = AreaInfo_v1[i].zonecount;
            name_ptr = &AreaInfo_v1[i].name;
        }
        else
        {
            type = AreaInfo_v2[i].type;
            zoneinfo_ptr = &AreaInfo_v2[i].zoneinfo;
            zonecount = AreaInfo_v2[i].zonecount;
            name_ptr = &AreaInfo_v2[i].name;
        }

        if(g_verbose)
        {
            printf("  [type=%u info=%#x count=%u name=%#x]\n", type, *zoneinfo_ptr,
                zonecount, *name_ptr);
        }
        /* translate name address */
        const char *name = (const char *)elf_reloc_addr32_ptr(name_ptr);
        if(name == nullptr || !elf_is_str_ptr_safe(name))
        {
            printf("  Entry name is not a string\n");
            continue;
        }
        /* skip reserved entries */
        if(*zoneinfo_ptr == 0)
        {
            printf("  %s\n", name);
            continue;
        }
        /* relocate the zoneinfo pointer */
        void *Zone = elf_reloc_addr32_ptr(zoneinfo_ptr);
        if(Zone == nullptr)
        {
            printf("  %s\n", name);
            printf("  Zone info pointer is not valid\n");
            continue;
        }
        /* in safe mode, make sure the zone info pointer is a symbol */
        Elf32_Sym *zoneinfo_sym = elf_get_symbol_by_ptr((void *)Zone);
        const char *zoneinfo_sym_name = "<no symbol>";
        if(zoneinfo_sym)
            zoneinfo_sym_name = elf_get_symbol_name(zoneinfo_sym);
        printf("  %s (%s)\n", name, zoneinfo_sym_name);
        if(!g_unsafe && !zoneinfo_sym)
        {
            printf("  Zone info pointer does not correspond to any symbol.\n");
            printf("  Use unsafe option to override this check\n");
            continue;
        }
        /* if we have the symbol, make sure the claimed size match */
        if(!g_unsafe && zoneinfo_sym)
        {
            if(zoneinfo_sym->st_size != sizeof_zone_info * zonecount)
            {
                printf("  Zone info symbol size (%u) does not match expected size (%zu)\n",
                    (unsigned)zoneinfo_sym->st_size, sizeof_zone_info * zonecount);
                printf("  Use unsafe option to override this check\n");
                continue;
            }
        }
        /* sanity check */
        if(!elf_is_ptr_safe((void *)Zone, sizeof_zone_info * zonecount))
        {
            printf("  Zone info pointer is not valid\n");
            continue;
        }
        /* read zone */
        zone_info_v1_t *Zone_v1 = (zone_info_v1_t *)Zone;
        zone_info_v2_t *Zone_v2 = (zone_info_v2_t *)Zone;
        for(size_t j = 0; j < zonecount; j++)
        {
            uint32_t node, start, count, size;
            uint32_t *name_ptr;

            if(ver == 1)
            {
                node = Zone_v1[j].node;
                start = Zone_v1[j].start;
                count = Zone_v1[j].count;
                size = Zone_v1[j].size;
                name_ptr = &Zone_v1[j].name;
            }
            else
            {
                node = Zone_v2[j].node;
                start = Zone_v2[j].start;
                count = Zone_v2[j].count;
                size = Zone_v2[j].size;
                name_ptr = &Zone_v2[j].name;
            }

            if(g_verbose)
            {
                printf("    [node=%u start=%#x count=%u size=%u name=%#x]\n",
                    node, start, count, size, *name_ptr);
            }
            /* translate name address */
            const char *name = (const char *)elf_reloc_addr32_ptr(name_ptr);
            if(name == nullptr || !elf_is_str_ptr_safe(name))
            {
                printf("    Entry name is not a string\n");
                continue;
            }
            printf("    %s: node %03u, size %u\n", name, node, size);
            if(fout)
                fprintf(fout, "%u,%u,%s\n", node, size, name);
        }
    }
    if(fout)
        fclose(fout);
    /* success */
    return 0;
}

int main(int argc, char **argv)
{
    const char *output = NULL;
    bool extract = false;

    if(argc <= 1)
        usage();

    while(1)
    {
        static struct option long_options[] =
        {
            {"help", no_argument, 0, 'h'},
            {"extract", no_argument, 0, 'x'},
            {"output", required_argument, 0, 'o'},
            {"verbose", no_argument, 0, 'v'},
            {"unsafe", no_argument, 0, 'u'},
            {0, 0, 0, 0}
        };

        int c = getopt_long(argc, argv, "hxo:vu", long_options, NULL);
        if(c == -1)
            break;
        switch(c)
        {
            case -1:
                break;
            case 'h':
                usage();
                break;
            case 'o':
                output = optarg;
                break;
            case 'x':
                extract = true;
                break;
            case 'v':
                g_verbose = true;
                break;
            case 'u':
                g_unsafe = true;
                break;
            default:
                abort();
        }
    }

    if(extract)
        return do_extract(output, argc - optind, argv + optind);
    printf("You need to specify an operation. Run nvptool -h for help\n");
    return 1;
}