Skip to content

Commit 1a2c90e

Browse files
committed
add apple partition table json output
1 parent 5eaf3e2 commit 1a2c90e

File tree

3 files changed

+82
-65
lines changed

3 files changed

+82
-65
lines changed

filesystem.c

+13-19
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ int fs_detail_fat(disk_t *disk, int indent, uint64_t sector)
152152

153153
drv_num = read_byte(buf + (bpb32 ? 64 : 36));
154154

155-
if(indent == 0) printf(SEP "\n");
155+
if(indent == 0) log_info(SEP "\n");
156156

157-
printf("%*sfat%u:\n", indent, "", fat_bits);
157+
log_info("%*sfat%u:\n", indent, "", fat_bits);
158158

159159
indent += 2;
160160

161-
printf("%*ssector size: %u\n", indent, "", bytes_p_sec);
161+
log_info("%*ssector size: %u\n", indent, "", bytes_p_sec);
162162

163-
printf(
163+
log_info(
164164
"%*sbpb[%u], oem \"%s\", media 0x%02x, drive 0x%02x, hs %u/%u\n", indent, "",
165165
bpb_len,
166166
cname(buf + 3, 8),
@@ -171,46 +171,46 @@ int fs_detail_fat(disk_t *disk, int indent, uint64_t sector)
171171
);
172172

173173
if(read_byte(buf + (bpb32 ? 66 : 38)) == 0x29) {
174-
printf("%*svol id 0x%08x, label \"%s\"", indent, "",
174+
log_info("%*svol id 0x%08x, label \"%s\"", indent, "",
175175
read_dword_le(buf + (bpb32 ? 67 : 39)),
176176
cname(buf + (bpb32 ? 71 : 43), 11)
177177
);
178-
printf(", fs type \"%s\"\n", cname(buf + (bpb32 ? 82 : 54), 8));
178+
log_info(", fs type \"%s\"\n", cname(buf + (bpb32 ? 82 : 54), 8));
179179
}
180180

181181
if(bpb32) {
182-
printf("%*sextflags 0x%02x, fs ver %u.%u, fs info %u, backup bpb %u\n", indent, "",
182+
log_info("%*sextflags 0x%02x, fs ver %u.%u, fs info %u, backup bpb %u\n", indent, "",
183183
read_byte(buf + 40),
184184
read_byte(buf + 43), read_byte(buf + 42),
185185
read_word_le(buf + 48),
186186
read_word_le(buf + 50)
187187
);
188188
}
189189

190-
printf("%*sfs size %u, hidden %u, data start %u\n", indent, "",
190+
log_info("%*sfs size %u, hidden %u, data start %u\n", indent, "",
191191
sectors,
192192
hidden,
193193
data_start
194194
);
195195

196-
printf("%*scluster size %u, clusters %u\n", indent, "",
196+
log_info("%*scluster size %u, clusters %u\n", indent, "",
197197
sec_p_cluster,
198198
clusters
199199
);
200200

201-
printf("%*sfats %u, fat size %u, fat start %u\n", indent, "",
201+
log_info("%*sfats %u, fat size %u, fat start %u\n", indent, "",
202202
fats,
203203
fat_secs,
204204
resvd_sec
205205
);
206206

207207
if(bpb32) {
208-
printf("%*sroot cluster %u\n", indent, "",
208+
log_info("%*sroot cluster %u\n", indent, "",
209209
read_dword_le(buf + 44)
210210
);
211211
}
212212
else {
213-
printf("%*sroot entries %u, root size %u, root start %u\n", indent, "",
213+
log_info("%*sroot entries %u, root size %u, root start %u\n", indent, "",
214214
root_ents,
215215
root_secs,
216216
resvd_sec + fats * fat_secs
@@ -237,7 +237,7 @@ int dump_fs(disk_t *disk, int indent, uint64_t sector)
237237
if(!fs_ok) return fs_ok;
238238

239239
if(indent == 0) {
240-
printf(SEP "\nfile system:\n");
240+
log_info(SEP "\nfile system:\n");
241241
indent += 2;
242242
}
243243

@@ -382,10 +382,4 @@ void read_isoinfo(disk_t *disk)
382382
free(line);
383383

384384
disk->block_size = current_block_size;
385-
386-
#if 0
387-
for(fs = iso_offsets; fs; fs = fs->next) {
388-
printf("block = %u, len = %u, name = '%s'\n", fs->block, fs->len, fs->name);
389-
}
390-
#endif
391385
}

ptable_apple.c

+40-17
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
#include <stdlib.h>
55
#include <unistd.h>
66
#include <string.h>
7-
#include <ctype.h>
8-
#include <iconv.h>
9-
#include <getopt.h>
107
#include <inttypes.h>
11-
#include <uuid/uuid.h>
12-
#include <blkid/blkid.h>
13-
#include <json-c/json.h>
148

159
#include "disk.h"
1610
#include "filesystem.h"
1711
#include "util.h"
12+
#include "json.h"
1813

1914
#include "ptable_apple.h"
2015

@@ -23,8 +18,6 @@ void dump_apple_ptables(disk_t *disk)
2318
for(disk->block_size = 0x200; disk->block_size <= 0x1000; disk->block_size <<= 1) {
2419
if(dump_apple_ptable(disk)) return;
2520
}
26-
27-
// printf("no apple partition table\n");
2821
}
2922

3023

@@ -44,31 +37,61 @@ int dump_apple_ptable(disk_t *disk)
4437

4538
parts = be32toh(apple->partitions);
4639

47-
printf(SEP "\napple partition table: %d entries\n", parts);
48-
printf(" sector size: %d\n", disk->block_size);
40+
json_object *json_apple = json_object_new_object();
41+
json_object_object_add(disk->json_disk, "apple", json_apple);
42+
43+
json_object_object_add(json_apple, "block_size", json_object_new_int(disk->block_size));
44+
json_object_object_add(json_apple, "entries", json_object_new_int(parts));
45+
46+
log_info(SEP "\napple partition table: %d entries\n", parts);
47+
log_info(" sector size: %d\n", disk->block_size);
48+
49+
json_object *json_table = json_object_new_array();
50+
json_object_object_add(json_apple, "partitions", json_table);
4951

5052
for(i = 1; i <= parts; i++) {
5153
if(disk_read(disk, buf, i, 1)) break;
5254
apple = (apple_entry_t *) buf;
55+
56+
json_object *json_entry = json_object_new_object();
57+
json_object_array_add(json_table, json_entry);
58+
59+
json_object_object_add(json_entry, "index", json_object_new_int64(i));
60+
json_object_object_add(json_entry, "number", json_object_new_int64(i));
61+
5362
u1 = be32toh(apple->start);
5463
u2 = be32toh(apple->size);
55-
printf("%3d %u - %llu (size %u)", i, u1, (unsigned long long) u1 + u2, u2);
64+
log_info("%3d %u - %llu (size %u)", i, u1, (unsigned long long) u1 + u2 - 1, u2);
65+
66+
json_object_object_add(json_entry, "first_lba", json_object_new_int64(u1));
67+
json_object_object_add(json_entry, "last_lba", json_object_new_int64(u1 + u2 - 1));
68+
json_object_object_add(json_entry, "size", json_object_new_int64(u2));
69+
5670
u1 = be32toh(apple->data_start);
5771
u2 = be32toh(apple->data_size);
58-
printf(", rel. %u - %llu (size %u)\n", u1, (unsigned long long) u1 + u2, u2);
72+
log_info(", rel. %u - %llu (size %u)\n", u1, (unsigned long long) u1 + u2, u2);
73+
74+
json_object_object_add(json_entry, "data_start", json_object_new_int64(u1));
75+
json_object_object_add(json_entry, "data_size", json_object_new_int64(u2));
76+
77+
json_object_object_add(json_entry, "status", json_object_new_int64(be32toh(apple->status)));
5978

6079
s = cname(apple->type, sizeof apple->type);
61-
printf(" type[%d] \"%s\"", (int) strlen(s), s);
80+
log_info(" type[%d] \"%s\"", (int) strlen(s), s);
81+
82+
json_object_object_add(json_entry, "type", json_object_new_string(s));
6283

63-
printf(", status 0x%x\n", be32toh(apple->status));
84+
log_info(", status 0x%x\n", be32toh(apple->status));
6485

6586
s = cname(apple->name, sizeof apple->name);
66-
printf(" name[%d] \"%s\"\n", (int) strlen(s), s);
87+
log_info(" name[%d] \"%s\"\n", (int) strlen(s), s);
6788

89+
json_object_object_add(json_entry, "name", json_object_new_string(s));
90+
91+
disk->json_current = json_entry;
6892
dump_fs(disk, 5, be32toh(apple->start));
93+
disk->json_current = disk->json_disk;
6994
}
7095

7196
return 1;
7297
}
73-
74-

zipl.c

+29-29
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void dump_zipl_components(disk_t *disk, uint64_t sec)
3737

3838
// compare including terminating 0 (header type 0 = ZIPL_COMP_HEADER_IPL)
3939
if(i || memcmp(buf, ZIPL_MAGIC, sizeof ZIPL_MAGIC)) {
40-
printf(" no components\n");
40+
log_info(" no components\n");
4141
return;
4242
}
4343

@@ -47,9 +47,9 @@ void dump_zipl_components(disk_t *disk, uint64_t sec)
4747
type = read_byte(buf + i * 0x20 + 0x17);
4848
load = read_qword_be(buf + i * 0x20 + 0x18);
4949
if(!load) break;
50-
printf(" %u start %llu", i - 1, (unsigned long long) start);
51-
if((size != disk->block_size && type == 2) || opt.show.raw) printf(", blksize %d", size);
52-
printf(
50+
log_info(" %u start %llu", i - 1, (unsigned long long) start);
51+
if((size != disk->block_size && type == 2) || opt.show.raw) log_info(", blksize %d", size);
52+
log_info(
5353
", addr 0x%016llx, type %d%s\n",
5454
(unsigned long long) load,
5555
type,
@@ -63,16 +63,16 @@ void dump_zipl_components(disk_t *disk, uint64_t sec)
6363
size2 = read_word_be(buf2 + k * 0x10 + 8);
6464
len2 = read_word_be(buf2 + k * 0x10 + 10) + 1;
6565
if(!start2) break;
66-
printf(
66+
log_info(
6767
" => start %llu, size %u",
6868
(unsigned long long) start2,
6969
len2
7070
);
71-
if(size2 != disk->block_size || opt.show.raw) printf(", blksize %d", size2);
71+
if(size2 != disk->block_size || opt.show.raw) log_info(", blksize %d", size2);
7272
if((s = iso_block_to_name(disk, start2))) {
73-
printf(", \"%s\"", s);
73+
log_info(", \"%s\"", s);
7474
}
75-
printf("\n");
75+
log_info("\n");
7676
}
7777

7878
// read it again
@@ -87,7 +87,7 @@ void dump_zipl_components(disk_t *disk, uint64_t sec)
8787
zh.extra = read_qword_be(buf3 + 0x20);
8888
zh.flags = read_word_be(buf3 + 0x28);
8989
zh.ok = 1;
90-
printf(
90+
log_info(
9191
" <zIPL stage3>\n"
9292
" parm 0x%016llx, initrd 0x%016llx (len %llu)\n"
9393
" psw 0x%016llx, extra %llu, flags 0x%x\n",
@@ -101,35 +101,35 @@ void dump_zipl_components(disk_t *disk, uint64_t sec)
101101
}
102102

103103
if((load | ZIPL_PSW_LOAD) == zh.psw ) {
104-
printf(" <kernel>\n");
104+
log_info(" <kernel>\n");
105105
}
106106

107107
if(load == zh.initrd_addr ) {
108-
printf(" <initrd>\n");
108+
log_info(" <initrd>\n");
109109
}
110110

111111
if(load == zh.parm_addr ) {
112-
printf(" <parm>\n");
112+
log_info(" <parm>\n");
113113
if(!disk_read(disk, buf3, start2, 1)) {
114114
unsigned char *s = buf3;
115115
buf3[sizeof buf3 - 1] = 0;
116-
printf(" \"");
116+
log_info(" \"");
117117
while(*s) {
118118
if(*s == '\n') {
119-
printf("\\n");
119+
log_info("\\n");
120120
}
121121
else if(*s == '\\' || *s == '"'/*"*/) {
122-
printf("\\%c", *s);
122+
log_info("\\%c", *s);
123123
}
124124
else if(*s >= 0x20 && *s < 0x7f) {
125-
printf("%c", *s);
125+
log_info("%c", *s);
126126
}
127127
else {
128-
printf("\\x%02x", *s);
128+
log_info("\\x%02x", *s);
129129
}
130130
s++;
131131
}
132-
printf("\"\n");
132+
log_info("\"\n");
133133
}
134134
}
135135
}
@@ -138,7 +138,7 @@ void dump_zipl_components(disk_t *disk, uint64_t sec)
138138

139139
if(type == 1) {
140140
if(load == (ZIPL_PSW_LOAD | 0xa050)) {
141-
printf(" <zipl stage3 entry>\n");
141+
log_info(" <zipl stage3 entry>\n");
142142
}
143143
}
144144
}
@@ -157,9 +157,9 @@ void dump_zipl(disk_t *disk)
157157

158158
if(i || memcmp(buf, ZIPL_MAGIC, sizeof ZIPL_MAGIC - 1)) return;
159159

160-
printf(SEP "\nzIPL (SCSI scheme):\n");
160+
log_info(SEP "\nzIPL (SCSI scheme):\n");
161161

162-
printf(
162+
log_info(
163163
" sector size: %d\n version: %u\n",
164164
disk->block_size,
165165
read_dword_be(buf + 4)
@@ -168,27 +168,27 @@ void dump_zipl(disk_t *disk)
168168
pt_sec = read_qword_be(buf + 0x10);
169169
size = read_word_be(buf + 0x18);
170170

171-
printf(" program table: %llu", (unsigned long long) pt_sec);
172-
if(size != disk->block_size || opt.show.raw) printf(", blksize %u", size);
171+
log_info(" program table: %llu", (unsigned long long) pt_sec);
172+
if(size != disk->block_size || opt.show.raw) log_info(", blksize %u", size);
173173
if((s = iso_block_to_name(disk, pt_sec))) {
174-
printf(", \"%s\"", s);
174+
log_info(", \"%s\"", s);
175175
}
176-
printf("\n");
176+
log_info("\n");
177177

178178
i = disk_read(disk, buf, pt_sec, 1);
179179

180180
if(i || memcmp(buf, ZIPL_MAGIC, sizeof ZIPL_MAGIC - 1)) {
181-
printf(" invalid program table\n");
181+
log_info(" invalid program table\n");
182182
return;
183183
}
184184

185185
for(i = 1; i < disk->block_size/16; i++) {
186186
sec = read_qword_be(buf + i * 0x10);
187187
size = read_word_be(buf + i * 0x10 + 8);
188188
if(!sec) break;
189-
printf(" %-3d start %llu", i - 1, (unsigned long long) sec);
190-
if(size != disk->block_size || opt.show.raw) printf(", blksize %u", size);
191-
printf(", components:\n");
189+
log_info(" %-3d start %llu", i - 1, (unsigned long long) sec);
190+
if(size != disk->block_size || opt.show.raw) log_info(", blksize %u", size);
191+
log_info(", components:\n");
192192
dump_zipl_components(disk, sec);
193193
}
194194
}

0 commit comments

Comments
 (0)