aboutsummaryrefslogtreecommitdiffstats
path: root/src/config-rom-pretty-printer.c
blob: 80ceb8fff89af2bfca605ff57616f19c26d76b9b (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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
// config-rom-pretty-printer.c - Pretty printer for content of configuration
// rom
//
// Copyright 2023 Takashi Sakamoto <o-takashi@sakamocchi.jp>
//
// licensed under the terms of the GNU General Public License, version 2

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include <fcntl.h>
#include <unistd.h>

#include <assert.h>
#include <endian.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#include <sys/queue.h>

#define CONST_ARRAY_SIZE(entries) (sizeof(entries) / sizeof(entries[0]))

#define LINE_WIDTH                100

//////////////////////////////////////////////////////////////
// Helpers to detect layout of blocks according to IEEE 1212.
//////////////////////////////////////////////////////////////

enum ieee1212_block_type {
    IEEE1212_BUS_INFO_BLOCK = 0,
    IEEE1212_ROOT_DIRECTORY_BLOCK,
    IEEE1212_LEAF_BLOCK,
    IEEE1212_DIRECTORY_BLOCK,
    ORPHAN_BLOCK,
};

struct ieee1212_block {
    size_t offset;
    size_t length;
    enum ieee1212_block_type block_type;
    const uint8_t *content;
    union {
        struct {
            uint8_t key_id;
            const struct ieee1212_block *parent;
        } leaf;
        struct {
            uint8_t key_id;
            const struct ieee1212_block *parent;
        } directory;
    } data;
    LIST_ENTRY(ieee1212_block) list;
};

LIST_HEAD(list_head, ieee1212_block);

static void insert_block_by_offset(struct list_head *head, struct ieee1212_block *entry)
{
    if (LIST_EMPTY(head)) {
        LIST_INSERT_HEAD(head, entry, list);
    } else {
        struct ieee1212_block *cursor = LIST_FIRST(head);

        while (true) {
            struct ieee1212_block *peek = LIST_NEXT(cursor, list);

            if (peek == NULL || peek->offset > entry->offset) {
                LIST_INSERT_AFTER(cursor, entry, list);
                break;
            }
            cursor = peek;
        }
    }
}

#define IEEE1212_BUS_INFO_BLOCK_LENGTH_MASK  0xff000000
#define IEEE1212_BUS_INFO_BLOCK_LENGTH_SHIFT 24
#define IEEE1212_BUS_INFO_CRC_LENGTH_MASK    0x00ff0000
#define IEEE1212_BUS_INFO_CRC_LENGTH_SHIFT   16
#define IEEE1212_BUS_INFO_CRC_MASK           0x0000ffff
#define IEEE1212_BUS_INFO_CRC_SHIFT          0

static int detect_ieee1212_bus_info_block_length(const uint8_t *data, ssize_t length, size_t offset,
                                                 size_t *block_length)
{
    uint32_t quadlet = ((uint32_t *)data)[0];

    *block_length = 4;
    *block_length += 4 * (quadlet & IEEE1212_BUS_INFO_BLOCK_LENGTH_MASK) >>
                     IEEE1212_BUS_INFO_BLOCK_LENGTH_SHIFT;

    if (offset + *block_length > length)
        return -EINVAL;

    return 0;
}

static int detect_ieee1212_bus_info_block(const uint8_t *data, ssize_t length, size_t offset,
                                          size_t block_length, struct list_head *head)
{
    struct ieee1212_block *entry;

    entry = malloc(sizeof(*entry));
    if (entry == NULL)
        return -ENOMEM;

    entry->offset = offset;
    entry->length = block_length;
    entry->block_type = IEEE1212_BUS_INFO_BLOCK;
    entry->content = data + offset;

    insert_block_by_offset(head, entry);

    return 0;
}

#define IEEE1212_BLOCK_LENGTH_MASK  0xffff0000
#define IEEE1212_BLOCK_LENGTH_SHIFT 16
#define IEEE1212_BLOCK_CRC_MASK     0x0000ffff
#define IEEE1212_BLOCK_CRC_SHIFT    0

static int detect_ieee1212_block_length(const uint8_t *data, ssize_t length, size_t offset,
                                        size_t *block_length)
{
    uint32_t quadlet = ((uint32_t *)(data + offset))[0];

    *block_length = 4;
    *block_length += 4 * (quadlet & IEEE1212_BLOCK_LENGTH_MASK) >> IEEE1212_BLOCK_LENGTH_SHIFT;

    if (offset + *block_length > length)
        return -EINVAL;

    return 0;
}

static int detect_ieee1212_leaf_block(const uint8_t *data, ssize_t length, size_t block_offset,
                                      size_t block_length, uint8_t key_id,
                                      const struct ieee1212_block *parent, struct list_head *head)
{
    struct ieee1212_block *entry;

    LIST_FOREACH(entry, head, list)
    {
        if (entry->offset == block_offset)
            return 0;
    }

    entry = malloc(sizeof(*entry));
    if (entry == NULL)
        return -ENOMEM;

    entry->offset = block_offset;
    entry->length = block_length;
    entry->block_type = IEEE1212_LEAF_BLOCK;
    entry->content = data + block_offset;
    entry->data.leaf.key_id = key_id;
    entry->data.leaf.parent = parent;

    insert_block_by_offset(head, entry);

    return 0;
}

#define DIRECTORY_ENTRY_KEY_TYPE_MASK  0xc0000000
#define DIRECTORY_ENTRY_KEY_TYPE_SHIFT 30
#define DIRECTORY_ENTRY_KEY_ID_MASK    0x3f000000
#define DIRECTORY_ENTRY_KEY_ID_SHIFT   24
#define DIRECTORY_ENTRY_VALUE_MASK     0x00ffffff
#define DIRECTORY_ENTRY_VALUE_SHIFT    0

#define KEY_TYPE_IMMEDIATE             0
#define KEY_TYPE_CSR_OFFSET            1
#define KEY_TYPE_LEAF                  2
#define KEY_TYPE_DIRECTORY             3

static void decode_directory_entry(uint32_t quadlet, uint8_t *key_type, uint8_t *key_id,
                                   uint32_t *value)
{
    *key_type = (quadlet & DIRECTORY_ENTRY_KEY_TYPE_MASK) >> DIRECTORY_ENTRY_KEY_TYPE_SHIFT;
    *key_id = (quadlet & DIRECTORY_ENTRY_KEY_ID_MASK) >> DIRECTORY_ENTRY_KEY_ID_SHIFT;
    *value = (quadlet & DIRECTORY_ENTRY_VALUE_MASK) >> DIRECTORY_ENTRY_VALUE_SHIFT;
}

static int detect_ieee1212_directory_block(const uint8_t *data, ssize_t length, size_t offset,
                                           size_t block_length, uint8_t key_id,
                                           const struct ieee1212_block *parent,
                                           struct list_head *head);

static int detect_ieee1212_directory_entries(const uint8_t *data, ssize_t length,
                                             size_t directory_offset, size_t directory_length,
                                             const struct ieee1212_block *parent,
                                             struct list_head *head)
{
    size_t quadlet_count;
    int err;
    int i;

    directory_offset += 4;
    quadlet_count = (directory_length - 4) / 4;

    for (i = 0; i < quadlet_count; ++i) {
        size_t entry_offset = directory_offset + i * 4;
        uint32_t quadlet = *((uint32_t *)(data + entry_offset));
        uint8_t key_type;
        uint8_t key_id;
        uint32_t value;

        decode_directory_entry(quadlet, &key_type, &key_id, &value);

        switch (key_type) {
        case KEY_TYPE_LEAF:
        case KEY_TYPE_DIRECTORY: {
            int (*detect_block)(const uint8_t *data, ssize_t length, size_t block_offset,
                                size_t block_length, uint8_t key_id,
                                const struct ieee1212_block *parent, struct list_head *head);
            size_t block_offset;
            size_t block_length;

            block_offset = entry_offset + 4 * value;
            if (block_offset >= length)
                return -ENOSPC;

            err = detect_ieee1212_block_length(data, length, block_offset, &block_length);
            if (err < 0)
                return err;

            if (key_type == KEY_TYPE_LEAF)
                detect_block = detect_ieee1212_leaf_block;
            else
                detect_block = detect_ieee1212_directory_block;

            err = detect_block(data, length, block_offset, block_length, key_id, parent, head);
            if (err < 0)
                return err;
            break;
        }
        default:
            break;
        }
    }

    return 0;
}

static int detect_ieee1212_directory_block(const uint8_t *data, ssize_t length, size_t block_offset,
                                           size_t block_length, uint8_t key_id,
                                           const struct ieee1212_block *parent,
                                           struct list_head *head)
{
    struct ieee1212_block *entry;

    LIST_FOREACH(entry, head, list)
    {
        if (entry->offset == block_offset)
            return 0;
    }

    entry = malloc(sizeof(*entry));
    if (entry == NULL)
        return -ENOMEM;

    entry->offset = block_offset;
    entry->length = block_length;
    entry->block_type = IEEE1212_DIRECTORY_BLOCK;
    entry->content = data + block_offset;
    entry->data.directory.key_id = key_id;
    entry->data.directory.parent = parent;

    insert_block_by_offset(head, entry);

    return detect_ieee1212_directory_entries(data, length, block_offset, block_length, entry, head);
}

static int detect_ieee1212_root_directory_block(const uint8_t *data, ssize_t length,
                                                size_t block_offset, size_t block_length,
                                                struct list_head *head)
{
    struct ieee1212_block *entry;

    entry = malloc(sizeof(*entry));
    if (entry == NULL)
        return -ENOMEM;

    entry->offset = block_offset;
    entry->length = block_length;
    entry->block_type = IEEE1212_ROOT_DIRECTORY_BLOCK;
    entry->content = data + block_offset;

    insert_block_by_offset(head, entry);

    return detect_ieee1212_directory_entries(data, length, block_offset, block_length, entry, head);
}

static int detect_ieee1212_blocks(const uint8_t *data, ssize_t length, struct list_head *head)
{
    size_t offset = 0;
    size_t block_length;
    int err;

    err = detect_ieee1212_bus_info_block_length(data, length, offset, &block_length);
    if (err < 0)
        return err;

    err = detect_ieee1212_bus_info_block(data, length, offset, block_length, head);
    if (err < 0)
        return err;

    offset += block_length;
    err = detect_ieee1212_block_length(data, length, offset, &block_length);
    if (err < 0)
        return err;

    return detect_ieee1212_root_directory_block(data, length, offset, block_length, head);
}

static void normalize_blocks(struct list_head *head, size_t length)
{
    struct ieee1212_block *entry;

    entry = LIST_FIRST(head);
    while (entry != NULL) {
        struct ieee1212_block *peek = LIST_NEXT(entry, list);
        size_t next_offset;

        if (peek != NULL)
            next_offset = peek->offset;
        else
            next_offset = length;

        if (entry->offset + entry->length > next_offset)
            entry->length = next_offset - entry->offset;

        entry = peek;
    }
}

static int fulfill_orphan_blocks(const uint8_t *data, ssize_t length, struct list_head *head)
{
    struct ieee1212_block *entry;
    int err = 0;

    entry = LIST_FIRST(head);
    while (entry != NULL) {
        struct ieee1212_block *peek = LIST_NEXT(entry, list);
        size_t next_offset;

        if (peek != NULL)
            next_offset = peek->offset;
        else
            next_offset = length;

        if (entry->offset + entry->length >= next_offset) {
            entry = peek;
        } else {
            struct ieee1212_block *orphan;

            orphan = malloc(sizeof(*orphan));
            if (orphan == NULL)
                return -ENOMEM;

            orphan->offset = entry->offset + entry->length;
            orphan->length = next_offset - orphan->offset;
            orphan->block_type = ORPHAN_BLOCK;
            orphan->content = data + orphan->offset;

            insert_block_by_offset(head, orphan);
        }
    }

    return err;
}

static bool bus_info_block_is_big_endian(const uint8_t *data, ssize_t length, size_t offset)
{
    const uint32_t quadlet = ((uint32_t *)data)[offset + 1];

    return quadlet == 0x1394;
}

static int print_blocks(const uint8_t *data, ssize_t data_length, struct list_head *head);

int main(int argc, const char *argv[])
{
    struct list_head head;
    struct ieee1212_block *entry;

    int fd = fileno(stdin);
    int err;

    // The size of region for configuration rom is fixed in IEEE 1212.
    uint8_t data[1024];
    ssize_t length = 0;
    size_t offset = 0;

    bool is_big_endian;

    if (isatty(fd)) {
        fprintf(stderr, "A terminal is detected for standard input. Output from "
                        "any process or shell "
                        "redirection should be referred instead.\n");
        return EXIT_FAILURE;
    }

    length = read(fd, data, sizeof(data));
    if (length <= 0)
        return EXIT_FAILURE;

    is_big_endian = bus_info_block_is_big_endian(data, length, offset);
    if (is_big_endian) {
        uint32_t *quadlet = (uint32_t *)data;
        int i;

        for (i = 0; i < length / 4; ++i)
            quadlet[i] = be32toh(quadlet[i]);
    }

    LIST_INIT(&head);

    err = detect_ieee1212_blocks(data, length, &head);
    if (err < 0)
        goto end;
    normalize_blocks(&head, length);

    err = fulfill_orphan_blocks(data, length, &head);
    if (err < 0)
        goto end;

    err = print_blocks(data, length, &head);
end:
    entry = LIST_FIRST(&head);
    while (entry != NULL) {
        struct ieee1212_block *peek = LIST_NEXT(entry, list);
        free(entry);
        entry = peek;
    }
    LIST_INIT(&head);

    if (err < 0)
        return EXIT_FAILURE;

    return EXIT_SUCCESS;
}

////////////////////////////////////////
// Helpers to format content of blocks.
////////////////////////////////////////

#define IEEE1212_REGISTER_SPACE_ADDRESS 0xfffff0000000
#define IEEE1212_CONFIG_ROM_OFFSET      0x400

#define KEY_ID_CSR_DESCRIPTOR           0x01
#define KEY_ID_CSR_BUS_DEP_INFO         0x02
#define KEY_ID_CSR_VENDOR_INFO          0x03
#define KEY_ID_CSR_HARDWARE_VERSION     0x04
#define KEY_ID_CSR_MODULE_INFO          0x07
#define KEY_ID_CSR_NODE_CAPS            0x0c
#define KEY_ID_CSR_EUI_64               0x0d
#define KEY_ID_CSR_UNIT                 0x11
#define KEY_ID_CSR_SPECIFIER_ID         0x12
#define KEY_ID_CSR_VERSION              0x13
#define KEY_ID_CSR_DEP_INFO             0x14
#define KEY_ID_CSR_UNIT_LOCATION        0x15
#define KEY_ID_CSR_MODEL                0x17
#define KEY_ID_CSR_INSTANCE             0x18
#define KEY_ID_CSR_KEYWORD              0x19
#define KEY_ID_CSR_FEATURE              0x1au
#define KEY_ID_CSR_MODIFIABLE_DESC      0x1f
#define KEY_ID_CSR_DIRECTORY_ID         0x20

#define INVALID_KEY_ID                  0xff // 6 bits are allowed for valid key id.
#define INVALID_KEY_VALUE               0xffffffff // 24 bits are allowed for valid value.

#define UNSPECIFIED_ENTRY_NAME          "(unspecified)"

struct spec_identifier {
    uint32_t specifier_id;
    uint32_t version;
};

struct key_formatter {
    uint8_t key_type;
    uint8_t key_id;
    const char *key_id_name;
    union {
        size_t (*immediate)(char *buf, size_t length, uint32_t value);
        size_t (*leaf)(char **buf, size_t length, size_t offset, const uint32_t *quadlets,
                       size_t quadlet_count, const char *spec_name);
        size_t (*directory)(char **buf, size_t length, size_t offset, const uint32_t *quadlets,
                            size_t quadlet_count, const struct spec_identifier *identifier);
    } format_content;
};

static void detect_key_formatter(const struct key_formatter **formatter, const char **spec_name,
                                 const struct spec_identifier *identifier, uint32_t key_type,
                                 uint32_t key_id);

static size_t format_line_prefix(char *buf, size_t length, size_t offset, uint32_t quadlet,
                                 bool add_delimiter)
{
    size_t consumed;

    offset += IEEE1212_CONFIG_ROM_OFFSET;

    consumed = snprintf(buf, length, "%3lx  %08x", offset, quadlet);

    if (add_delimiter)
        consumed += snprintf(buf + consumed, length - consumed, "  ");

    return consumed;
}

static size_t format_blank_prefix(char *buf, size_t length)
{
    size_t consumed = snprintf(buf, length, "%3x  %08x  ", IEEE1212_CONFIG_ROM_OFFSET, 0u);
    memset(buf, ' ', consumed);
    return consumed;
}

static size_t format_horizontal_line(char *buf, size_t length)
{
    return snprintf(buf, length,
                    "-----------------------------------------------------------------");
}

static uint16_t compute_itu_t_crc_16(const uint32_t *quadlet, size_t quadlet_count)
{
    uint32_t crc = 0;
    int i;

    for (i = 0; i < quadlet_count; ++i) {
        int shift;

        for (shift = 28; shift >= 0; shift -= 4) {
            uint32_t sum = ((crc >> 12) ^ (quadlet[i] >> shift)) & 0x0000000f;

            crc = ((crc << 4) ^ (sum << 12) ^ (sum << 5) ^ sum) & 0x0000ffff;
        }
    }

    return (uint16_t)crc;
}

static size_t format_bus_info_metadata(char *buf, size_t length, const uint32_t *quadlet,
                                       size_t quadlet_count, size_t data_length)
{
    uint8_t block_length = (quadlet[0] & IEEE1212_BUS_INFO_BLOCK_LENGTH_MASK) >>
                           IEEE1212_BUS_INFO_BLOCK_LENGTH_SHIFT;
    uint8_t crc_length = (quadlet[0] & IEEE1212_BUS_INFO_CRC_LENGTH_MASK) >>
                         IEEE1212_BUS_INFO_CRC_LENGTH_SHIFT;
    uint16_t crc = (quadlet[0] & IEEE1212_BUS_INFO_CRC_MASK) >> IEEE1212_BUS_INFO_CRC_SHIFT;
    uint16_t actual_crc;
    size_t consumed;

    consumed = snprintf(buf, length, "bus_info_length %d", block_length);

    consumed += snprintf(buf + consumed, length - consumed, ", crc_length %d", crc_length);
    if (4 * (crc_length + 1) <= data_length) {
        actual_crc = compute_itu_t_crc_16(quadlet + 1, crc_length);
    } else {
        uint8_t effective_crc_length = (data_length - 4) / 4;
        consumed +=
            snprintf(buf + consumed, length - consumed, " (up to %d)", effective_crc_length);
        actual_crc = compute_itu_t_crc_16(quadlet + 1, effective_crc_length);
    }

    consumed += snprintf(buf + consumed, length - consumed, ", crc %d", crc);
    if (crc != actual_crc)
        consumed += snprintf(buf + consumed, length - consumed, " (should be %d)", actual_crc);

    return consumed;
}

static size_t format_ieee1394_bus_dependent_information(char **buf, size_t length, size_t offset,
                                                        uint32_t quadlet)
{
    bool irm_capable = (quadlet & 0x80000000) >> 31;
    bool cm_capable = (quadlet & 0x40000000) >> 30;
    bool is_capable = (quadlet & 0x20000000) >> 29;
    bool bm_capable = (quadlet & 0x10000000) >> 28;
    uint8_t cyc_clk_acc = (quadlet & 0x00ff0000) >> 16;
    uint8_t max_rec = (quadlet & 0x0000f000) >> 12;
    uint8_t generation = (quadlet & 0x000000f0) >> 4;
    size_t lines;
    size_t consumed;

    lines = 0;

    if (generation > 0) {
        bool pm_capable = (quadlet & 0x08000000) >> 27;
        uint8_t max_rom = (quadlet & 0x00000300) >> 8;
        uint8_t spd = quadlet & 0x00000007;

        consumed = format_line_prefix(buf[lines], length, offset, quadlet, true);
        snprintf(buf[lines] + consumed, length - consumed,
                 "irmc %d, cmc %d, isc %d, bmc %d, pmc %d, cyc_clk_acc %d,", irm_capable,
                 cm_capable, is_capable, bm_capable, pm_capable, cyc_clk_acc);
        ++lines;

        consumed = format_blank_prefix(buf[lines], length);
        snprintf(buf[lines] + consumed, length - consumed,
                 "max_rec %d (%d), max_rom %d, gen %d, spd %d (S%d00)", max_rec, 2 << max_rec,
                 max_rom, generation, spd, 1 << spd);
        ++lines;
    } else {
        consumed = format_line_prefix(buf[0], length, offset, quadlet, true);
        snprintf(buf[lines] + consumed, length - consumed,
                 "irmc %d, cmc %d, isc %d, bmc %d, cyc_clk_acc %d, max_rec %d (%d)", irm_capable,
                 cm_capable, is_capable, bm_capable, cyc_clk_acc, max_rec, 2 << max_rec);
        ++lines;
    }

    return lines;
}

static size_t format_unspecified_bus_dependent_information(char **buf, size_t length, size_t offset,
                                                           uint32_t quadlet)
{
    format_line_prefix(buf[0], length, offset, quadlet, false);

    return 1;
}

static size_t format_bus_info_block(char **buf, size_t length,
                                    const struct ieee1212_block *bus_info, size_t data_length)
{
    static const struct {
        uint32_t bus_name_value;
        const char *bus_name;
        size_t (*format)(char **buf, size_t length, size_t offset, uint32_t quadlet);
    } *bus_entry, bus_entries[] = {
        {
            0x31333934,
            "1394",
            format_ieee1394_bus_dependent_information,
        },
        {
            0xffffffff,
            "unspecified",
            format_unspecified_bus_dependent_information,
        },
    };
    size_t offset = bus_info->offset;
    const uint32_t *quadlet = (const uint32_t *)bus_info->content;
    size_t quadlet_count = bus_info->length / 4;
    size_t lines;
    size_t consumed;
    uint32_t company_id;
    uint64_t device_id;
    uint64_t eui64;
    int i;

    lines = 0;

    consumed = format_blank_prefix(buf[lines], length);
    snprintf(buf[lines] + consumed, length - consumed, "ROM header and bus information block");
    ++lines;

    consumed = format_blank_prefix(buf[lines], length);
    format_horizontal_line(buf[lines] + consumed, length - consumed);
    ++lines;

    consumed = format_line_prefix(buf[lines], length, offset, quadlet[0], true);
    format_bus_info_metadata(buf[lines] + consumed, length - consumed, quadlet, quadlet_count,
                             data_length);
    ++lines;

    bus_entry = NULL;
    for (i = 0; i < CONST_ARRAY_SIZE(bus_entries); ++i) {
        if (bus_entries[i].bus_name_value == quadlet[1]) {
            bus_entry = bus_entries + i;
            break;
        }
    }
    if (bus_entry == NULL)
        bus_entry = &bus_entries[CONST_ARRAY_SIZE(bus_entries) - 1];

    consumed = format_line_prefix(buf[lines], length, offset + 4, quadlet[1], true);
    snprintf(buf[lines] + consumed, length - consumed, "bus_name \"%s\"", bus_entry->bus_name);
    ++lines;

    lines += bus_entry->format(buf + lines, length, offset + 8, quadlet[2]);

    company_id = (quadlet[3] & 0xffffff00) >> 8;
    device_id = ((((uint64_t)quadlet[3]) & 0x000000ff) << 32) | quadlet[4];
    eui64 = (((uint64_t)quadlet[3]) << 32) | quadlet[4];

    consumed = format_line_prefix(buf[lines], length, offset + 12, quadlet[3], true);
    snprintf(buf[lines] + consumed, length - consumed, "company_id %06x     | ", company_id);
    ++lines;

    consumed = format_line_prefix(buf[lines], length, offset + 16, quadlet[4], true);
    snprintf(buf[lines] + consumed, length - consumed, "device_id %010lx  | EUI-64 %016lx",
             device_id, eui64);
    ++lines;

    if (quadlet_count > 5) {
        for (i = 5; i < quadlet_count; ++i) {
            format_line_prefix(buf[lines], length, offset + 4 * 5, quadlet[i], false);
            ++lines;
        }
    }

    return lines;
}

static size_t format_block_metadata(char *buf, size_t length, const char *block_name,
                                    const uint32_t *quadlet, size_t quadlet_count)
{
    uint16_t block_length = (quadlet[0] & IEEE1212_BLOCK_LENGTH_MASK) >>
                            IEEE1212_BLOCK_LENGTH_SHIFT;
    uint16_t block_crc = (quadlet[0] & IEEE1212_BLOCK_CRC_MASK) >> IEEE1212_BLOCK_CRC_SHIFT;
    uint16_t actual_block_crc = compute_itu_t_crc_16(quadlet + 1, quadlet_count - 1);
    size_t consumed;

    consumed = snprintf(buf, length, "%s_length %d", block_name, block_length);
    if (1 + block_length != quadlet_count)
        consumed +=
            snprintf(buf + consumed, length - consumed, " (actual length %ld)", quadlet_count - 1);

    consumed += snprintf(buf + consumed, length - consumed, ", crc %d", block_crc);
    if (block_crc != actual_block_crc)
        consumed +=
            snprintf(buf + consumed, length - consumed, " (should be %d)", actual_block_crc);

    return consumed;
}

static size_t format_leaf_block(char **buf, size_t length, const struct ieee1212_block *leaf,
                                size_t data_length)
{
    struct spec_identifier identifier = {
        INVALID_KEY_VALUE,
        INVALID_KEY_VALUE,
    };
    const struct ieee1212_block *base = leaf->data.leaf.parent;
    const struct key_formatter *formatter;
    const char *spec_name = NULL;
    size_t offset;
    const uint32_t *quadlet;
    size_t quadlet_count;
    size_t consumed;
    size_t lines;
    int i;

    while (base != NULL) {
        quadlet = (const uint32_t *)base->content;
        quadlet_count = base->length / 4;

        for (i = 1; i < quadlet_count; ++i) {
            uint8_t key_type;
            uint8_t key_id;
            uint32_t value;

            decode_directory_entry(quadlet[i], &key_type, &key_id, &value);

            if (key_type == KEY_TYPE_IMMEDIATE) {
                switch (key_id) {
                case KEY_ID_CSR_SPECIFIER_ID:
                    if (identifier.specifier_id == INVALID_KEY_VALUE)
                        identifier.specifier_id = value;
                    break;
                case KEY_ID_CSR_VERSION:
                    if (identifier.version == INVALID_KEY_VALUE)
                        identifier.version = value;
                    break;
                case KEY_ID_CSR_VENDOR_INFO:
                    if (identifier.specifier_id == INVALID_KEY_VALUE)
                        identifier.specifier_id = value;
                default:
                    break;
                }
            }
        }

        if (base->block_type == IEEE1212_DIRECTORY_BLOCK)
            base = base->data.directory.parent;
        else
            base = NULL;
    }

    detect_key_formatter(&formatter, &spec_name, &identifier, KEY_TYPE_LEAF,
                         leaf->data.leaf.key_id);

    offset = leaf->offset;
    quadlet = (const uint32_t *)leaf->content;
    quadlet_count = leaf->length / 4;

    lines = 0;

    consumed = format_blank_prefix(buf[lines], length);
    if (spec_name != NULL)
        consumed += snprintf(buf[lines] + consumed, length - consumed, "%s ", spec_name);
    snprintf(buf[lines] + consumed, length - consumed, "%s leaf at %lx", formatter->key_id_name,
             IEEE1212_CONFIG_ROM_OFFSET + offset);
    ++lines;

    consumed = format_blank_prefix(buf[lines], length);
    format_horizontal_line(buf[lines] + consumed, length - consumed);
    ++lines;

    consumed = format_line_prefix(buf[lines], length, offset, quadlet[0], true);
    format_block_metadata(buf[lines] + consumed, length - consumed, "leaf", quadlet, quadlet_count);
    ++lines;

    offset += 4;
    ++quadlet;
    --quadlet_count;

    lines +=
        formatter->format_content.leaf(buf + lines, length, offset, quadlet, quadlet_count, NULL);

    return lines;
}

static size_t format_entry_spec_name(char *buf, size_t length, const char *spec_name)
{
    if (spec_name != NULL)
        return snprintf(buf, length, "%s ", spec_name);

    return 0;
}

static size_t format_immediate_entry(char *buf, size_t length, size_t offset, uint32_t value,
                                     const char *spec_name, const struct key_formatter *formatter)
{
    size_t consumed;

    consumed = format_entry_spec_name(buf, length, spec_name);

    if (formatter->key_id != INVALID_KEY_ID)
        consumed += snprintf(buf + consumed, length - consumed, "%s", formatter->key_id_name);

    if (formatter->format_content.immediate != NULL) {
        if (formatter->key_id != INVALID_KEY_ID)
            consumed += snprintf(buf + consumed, length - consumed, ": ");
        consumed += formatter->format_content.immediate(buf + consumed, length - consumed, value);
    }

    return consumed;
}

static size_t format_csr_offset_entry(char *buf, size_t length, size_t offset, uint32_t value,
                                      const char *spec_name, const struct key_formatter *formatter)
{
    size_t csr_offset = IEEE1212_REGISTER_SPACE_ADDRESS + 4 * value;
    size_t consumed;

    consumed = snprintf(buf, length, "--> ");
    consumed += format_entry_spec_name(buf + consumed, length - consumed, spec_name);

    if (formatter->key_id != INVALID_KEY_ID)
        consumed += snprintf(buf + consumed, length - consumed, "%s ", formatter->key_id_name);
    else
        consumed += snprintf(buf + consumed, length - consumed, "CSR ");

    consumed += snprintf(buf + consumed, length - consumed, "at %012lx", csr_offset);

    return consumed;
}

static size_t format_leaf_entry(char *buf, size_t length, size_t offset, uint32_t value,
                                const char *spec_name, const struct key_formatter *formatter)
{
    size_t leaf_offset = IEEE1212_CONFIG_ROM_OFFSET + offset + 4 * value;
    size_t consumed;

    consumed = snprintf(buf, length, "--> ");
    consumed += format_entry_spec_name(buf + consumed, length - consumed, spec_name);

    if (formatter->key_id != INVALID_KEY_ID)
        consumed += snprintf(buf + consumed, length - consumed, "%s ", formatter->key_id_name);

    consumed += snprintf(buf + consumed, length - consumed, "leaf at %lx", leaf_offset);

    return consumed;
}

static size_t format_directory_entry(char *buf, size_t length, size_t offset, uint32_t value,
                                     const char *spec_name, const struct key_formatter *formatter)
{
    size_t directory_offset = IEEE1212_CONFIG_ROM_OFFSET + offset + 4 * value;
    size_t consumed;

    consumed = snprintf(buf, length, "--> ");
    consumed += format_entry_spec_name(buf + consumed, length - consumed, spec_name);

    if (formatter->key_id != INVALID_KEY_ID)
        consumed += snprintf(buf + consumed, length - consumed, "%s ", formatter->key_id_name);

    consumed += snprintf(buf + consumed, length - consumed, "directory at %lx", directory_offset);

    return consumed;
}

static size_t format_directory_entries(char **buf, size_t length, size_t directory_offset,
                                       const uint32_t *quadlet, size_t quadlet_count,
                                       const struct spec_identifier *identifier)
{
    static size_t (*const format_entry[])(char *buf, size_t length, size_t offset, uint32_t value,
                                          const char *spec_name,
                                          const struct key_formatter *formatter) = {
        [KEY_TYPE_IMMEDIATE] = format_immediate_entry,
        [KEY_TYPE_CSR_OFFSET] = format_csr_offset_entry,
        [KEY_TYPE_LEAF] = format_leaf_entry,
        [KEY_TYPE_DIRECTORY] = format_directory_entry,
    };
    size_t consumed;
    int i;

    consumed = format_line_prefix(buf[0], length, directory_offset, quadlet[0], true);
    format_block_metadata(buf[0] + consumed, length - consumed, "directory", quadlet,
                          quadlet_count);

    for (i = 1; i < quadlet_count; ++i) {
        const struct key_formatter *formatter;
        size_t offset = directory_offset + i * 4;
        uint8_t key_type;
        uint8_t key_id;
        uint32_t value;
        size_t consumed;
        const char *spec_name = NULL;

        decode_directory_entry(quadlet[i], &key_type, &key_id, &value);

        detect_key_formatter(&formatter, &spec_name, identifier, key_type, key_id);

        consumed = format_line_prefix(buf[i], length, offset, quadlet[i], true);
        format_entry[key_type](buf[i] + consumed, length - consumed, offset, value, spec_name,
                               formatter);
    }

    return quadlet_count;
}

static size_t format_directory_block(char **buf, size_t length,
                                     const struct ieee1212_block *directory, size_t data_length)
{
    struct spec_identifier identifier = {
        INVALID_KEY_VALUE,
        INVALID_KEY_VALUE,
    };
    const struct ieee1212_block *base;
    size_t offset;
    const uint32_t *quadlet;
    size_t quadlet_count;
    const struct key_formatter *formatter;
    const char *spec_name = NULL;
    size_t lines;
    size_t consumed;
    int i;

    switch (directory->data.directory.key_id) {
    case KEY_ID_CSR_VENDOR_INFO:
    case KEY_ID_CSR_MODULE_INFO:
    case KEY_ID_CSR_DESCRIPTOR:
    case KEY_ID_CSR_BUS_DEP_INFO:
    case KEY_ID_CSR_DEP_INFO:
    case KEY_ID_CSR_INSTANCE:
        base = directory->data.directory.parent;
        break;
    case KEY_ID_CSR_UNIT:
    case KEY_ID_CSR_FEATURE:
        base = directory;
        break;
    default:
        base = NULL;
        break;
    }

    while (base != NULL) {
        quadlet = (const uint32_t *)base->content;
        quadlet_count = base->length / 4;

        for (i = 1; i < quadlet_count; ++i) {
            uint8_t key_type;
            uint8_t key_id;
            uint32_t value;

            decode_directory_entry(quadlet[i], &key_type, &key_id, &value);

            if (key_type == KEY_TYPE_IMMEDIATE) {
                switch (key_id) {
                case KEY_ID_CSR_SPECIFIER_ID:
                    if (identifier.specifier_id == INVALID_KEY_VALUE)
                        identifier.specifier_id = value;
                    break;
                case KEY_ID_CSR_VERSION:
                    if (identifier.version == INVALID_KEY_VALUE)
                        identifier.version = value;
                    break;
                case KEY_ID_CSR_VENDOR_INFO:
                    if (identifier.specifier_id == INVALID_KEY_VALUE)
                        identifier.specifier_id = value;
                default:
                    break;
                }
            }
        }

        if (base->block_type == IEEE1212_DIRECTORY_BLOCK)
            base = base->data.directory.parent;
        else
            base = NULL;
    }

    offset = directory->offset;
    quadlet = (const uint32_t *)directory->content;
    quadlet_count = directory->length / 4;

    detect_key_formatter(&formatter, &spec_name, &identifier, KEY_TYPE_DIRECTORY,
                         directory->data.directory.key_id);

    lines = 0;

    consumed = format_blank_prefix(buf[lines], length);
    snprintf(buf[lines] + consumed, length - consumed, "%s directory at %lx",
             formatter->key_id_name, IEEE1212_CONFIG_ROM_OFFSET + offset);
    ++lines;

    consumed = format_blank_prefix(buf[lines], length);
    format_horizontal_line(buf[lines] + consumed, length - consumed);
    ++lines;

    lines += formatter->format_content.directory(buf + lines, length, offset, quadlet,
                                                 quadlet_count, &identifier);

    return lines;
}

static size_t format_root_directory_block(char **buf, size_t length,
                                          const struct ieee1212_block *root, size_t data_length)
{
    struct spec_identifier identifier = {
        INVALID_KEY_VALUE,
        INVALID_KEY_VALUE,
    };
    size_t offset = root->offset;
    const uint32_t *quadlet = (const uint32_t *)root->content;
    size_t quadlet_count = root->length / 4;
    size_t lines;
    size_t consumed;
    int i;

    for (i = 1; i < quadlet_count; ++i) {
        uint8_t key_type;
        uint8_t key_id;
        uint32_t value;

        decode_directory_entry(quadlet[i], &key_type, &key_id, &value);

        if (key_type == KEY_TYPE_IMMEDIATE && key_id == KEY_ID_CSR_VENDOR_INFO)
            identifier.specifier_id = value;
    }

    lines = 0;

    consumed = format_blank_prefix(buf[lines], length);
    snprintf(buf[lines] + consumed, length - consumed, "root directory");
    ++lines;

    consumed = format_blank_prefix(buf[lines], length);
    format_horizontal_line(buf[lines] + consumed, length - consumed);
    ++lines;

    lines +=
        format_directory_entries(buf + lines, length, offset, quadlet, quadlet_count, &identifier);

    return lines;
}

static size_t format_orphan_block(char **buf, size_t length, const struct ieee1212_block *orphan,
                                  size_t data_length)
{
    const uint32_t *quadlet = (const uint32_t *)orphan->content;
    size_t quadlet_count = orphan->length / 4;
    int i;

    for (i = 0; i < quadlet_count; ++i) {
        size_t offset = orphan->offset + i * 4;
        size_t consumed = format_line_prefix(buf[i], length, offset, quadlet[i], true);
        snprintf(buf[i] + consumed, length - consumed, "(unreferenced data)");
    }

    return i;
}

static int print_blocks(const uint8_t *data, ssize_t data_length, struct list_head *head)
{
    static size_t (*const format[])(char **buf, size_t length, const struct ieee1212_block *entry,
                                    size_t data_length) = {
        format_bus_info_block,  format_root_directory_block, format_leaf_block,
        format_directory_block, format_orphan_block,
    };
    struct ieee1212_block *block;
    size_t lines;
    char **buf;
    size_t offset;
    int i;

    lines = 0;
    LIST_FOREACH(block, head, list)
    {
        size_t count = block->length / 4;
        if (lines < count)
            lines = count;
    }
    // Add extra 10 lines for safe.
    lines += 10;

    buf = calloc(sizeof(*buf), lines);
    if (buf == NULL)
        return -ENOMEM;
    for (i = 0; i < lines; ++i) {
        buf[i] = malloc(LINE_WIDTH);
        if (buf[i] == NULL)
            goto end;
    }

    offset = 0;
    LIST_FOREACH(block, head, list)
    {
        size_t count = format[block->block_type](buf, LINE_WIDTH, block, data_length);
        for (i = 0; i < count; ++i)
            printf("%s\n", buf[i]);
        printf("\n");

        offset += block->length;
    }

end:
    for (i = 0; i < lines; ++i) {
        if (buf[i] != NULL)
            free(buf[i]);
    }
    free(buf);

    return 0;
}

//////////////
// Protocols.
//////////////

#define OUI_ICANN_IANA        0x00005e
#define SPEC_VERSION_RFC_2734 0x000001
#define SPEC_VERSION_RFC_3146 0x000002
#define SPEC_NAME_RFC_2734    "IPv4 over 1394 (RFC 2734)"
#define SPEC_NAME_RFC_3146    "IPv6 over 1394 (RFC 3146)"

static const struct spec_identifier spec_iana_ipv4 = {
    OUI_ICANN_IANA,
    SPEC_VERSION_RFC_2734,
};

static const struct spec_identifier spec_iana_ipv6 = {
    OUI_ICANN_IANA,
    SPEC_VERSION_RFC_3146,
};

#define OUI_INCITS           0x00609e
#define SPEC_VERSION_SBP     0x010483
#define SPEC_VERSION_SBP_AVC 0x0105bb
#define SPEC_NAME_SBP        "SBP-2"
#define SPEC_NAME_SBP_AVC    "AV/C over SBP-3"

static const struct spec_identifier spec_incits_sbp = {
    OUI_INCITS,
    SPEC_VERSION_SBP,
};

static const struct spec_identifier spec_incits_sbp_avc = {
    OUI_INCITS,
    SPEC_VERSION_SBP_AVC,
};

#define OUI_1394TA                     0x00a02d
#define SPEC_VERSION_AVC               0x010001
#define SPEC_VERSION_CAL               0x010002
#define SPEC_VERSION_EHS               0x010004
#define SPEC_VERSION_HAVI              0x010008
#define SPEC_VERSION_VENDOR_UNIQUE     0x014000
#define SPEC_VERSION_VENDOR_UNIQUE_AVC 0x014001
#define SPEC_VERSION_IIDC_104          0x000100
#define SPEC_VERSION_IIDC_120          0x000101
#define SPEC_VERSION_IIDC_130          0x000102
#define SPEC_VERSION_IIDC2             0x000110
#define SPEC_VERSION_DPP_111           0x0a6be2
#define SPEC_VERSION_IICP              0x4b661f
#define SPEC_NAME_AVC                  "AV/C"
#define SPEC_NAME_CAL                  "CAL"
#define SPEC_NAME_EHS                  "EHS"
#define SPEC_NAME_HAVI                 "HAVi"
#define SPEC_NAME_VENDOR_UNIQUE        "Vendor Unique"
#define SPEC_NAME_VENDOR_UNIQUE_AVC    "Vendor Unique and AV/C"
#define SPEC_NAME_IIDC_104             "IIDC 1.04"
#define SPEC_NAME_IIDC_120             "IIDC 1.20"
#define SPEC_NAME_IIDC_130             "IIDC 1.30"
#define SPEC_NAME_IIDC2                "IIDC2"
#define SPEC_NAME_DPP_111              "DPP 1.0"
#define SPEC_NAME_IICP                 "IICP 1.0"

static const struct spec_identifier spec_1394ta_avc = {
    OUI_1394TA,
    SPEC_VERSION_AVC,
};

static const struct spec_identifier spec_1394ta_cal = {
    OUI_1394TA,
    SPEC_VERSION_CAL,
};

static const struct spec_identifier spec_1394ta_ehs = {
    OUI_1394TA,
    SPEC_VERSION_EHS,
};

static const struct spec_identifier spec_1394ta_havi = {
    OUI_1394TA,
    SPEC_VERSION_HAVI,
};

static const struct spec_identifier spec_1394ta_vendor_unique = {
    OUI_1394TA,
    SPEC_VERSION_VENDOR_UNIQUE,
};

static const struct spec_identifier spec_1394ta_vendor_unique_avc = {
    OUI_1394TA,
    SPEC_VERSION_VENDOR_UNIQUE_AVC,
};

static const struct spec_identifier spec_1394ta_iidc_104 = {
    OUI_1394TA,
    SPEC_VERSION_IIDC_104,
};

static const struct spec_identifier spec_1394ta_iidc_120 = {
    OUI_1394TA,
    SPEC_VERSION_IIDC_120,
};

static const struct spec_identifier spec_1394ta_iidc_130 = {
    OUI_1394TA,
    SPEC_VERSION_IIDC_130,
};

static const struct spec_identifier spec_1394ta_iidc2 = {
    OUI_1394TA,
    SPEC_VERSION_IIDC2,
};

static const struct spec_identifier spec_1394ta_dpp_111 = {
    OUI_1394TA,
    SPEC_VERSION_DPP_111,
};

static const struct spec_identifier spec_1394ta_iicp = {
    OUI_1394TA,
    SPEC_VERSION_IICP,
};

#define OUI_ALESIS                0x000595
#define SPEC_VERSION_ALESIS_AUDIO 0x000001
#define SPEC_NAME_ALESIS_AUDIO    "audio"

static const struct spec_identifier spec_alesis_audio = {
    OUI_ALESIS,
    SPEC_VERSION_ALESIS_AUDIO,
};

#define OUI_APPLE                   0x000a27
#define SPEC_VERSION_ISIGHT_AUDIO   0x000010
#define SPEC_VERSION_ISIGHT_FACTORY 0x000011
#define SPEC_VERSION_ISIGHT_IRIS    0x000012
#define SPEC_NAME_ISIGHT_AUDIO      "iSight audio unit"
#define SPEC_NAME_ISIGHT_FACTORY    "iSight factory unit"
#define SPEC_NAME_ISIGHT_IRIS       "iSight iris unit"

static const struct spec_identifier spec_apple_isight_audio = {
    OUI_APPLE,
    SPEC_VERSION_ISIGHT_AUDIO,
};

static const struct spec_identifier spec_apple_isight_factory = {
    OUI_APPLE,
    SPEC_VERSION_ISIGHT_FACTORY,
};

static const struct spec_identifier spec_apple_isight_iris = {
    OUI_APPLE,
    SPEC_VERSION_ISIGHT_IRIS,
};

#define OUI_LACIE              0x00d04b
#define SPEC_VERSION_LACIE_HID 0x484944
#define SPEC_NAME_LACIE_HID    "HID"

static const struct spec_identifier spec_lacie_hid = {
    OUI_LACIE,
    SPEC_VERSION_LACIE_HID,
};

///////////////////////////////////////////////
// Directory entries specific to CSR directory.
///////////////////////////////////////////////

#define CSR_DESCRIPTOR_NAME       "descriptor"
#define CSR_BUS_DEP_INFO_NAME     "bus dependent info"
#define CSR_VENDOR_INFO_NAME      "vendor"
#define CSR_HARDWARE_VERSION_NAME "hardware version"
#define CSR_MODULE_INFO_NAME      "module"
#define CSR_NODE_CAPS_NAME        "node capabilities"
#define CSR_EUI_64_NAME           "eui-64"
#define CSR_UNIT_NAME             "unit"
#define CSR_SPECIFIER_ID_NAME     "specifier id"
#define CSR_VERSION_NAME          "version"
#define CSR_DEP_INFO_NAME         "dependent info"
#define CSR_UNIT_LOCATION_NAME    "unit location"
#define CSR_MODEL_NAME            "model"
#define CSR_INSTANCE_NAME         "instance"
#define CSR_KEYWORD_NAME          "keyword"
#define CSR_FEATURE_NAME          "feature"
#define CSR_MODIFIABLE_DESC_NAME  "modifiable descriptor"
#define CSR_DIRECTORY_ID_NAME     "directory id"

static size_t format_csr_textual_descriptor_leaf_content(char **buf, size_t length, size_t offset,
                                                         const uint32_t *quadlet,
                                                         size_t quadlet_count,
                                                         const char *spec_name)
{
    uint8_t width;
    uint16_t character_set;
    uint16_t language;
    size_t consumed;
    int i, j;

    if (quadlet_count < 2)
        return 0;

    width = quadlet[0] >> 28;
    character_set = (quadlet[0] & 0x0fff0000) >> 16;
    language = (quadlet[0] & 0x0000ffff) >> 0;

    consumed = format_line_prefix(buf[0], length, offset, quadlet[0], true);
    if (character_set == 0) {
        snprintf(buf[0] + consumed, length - consumed, "minimal ASCII");
    } else {
        snprintf(buf[0] + consumed, length - consumed, "width %d, character_set %d, language %d",
                 width, character_set, language);
    }

    for (i = 1; i < quadlet_count; ++i) {
        consumed = format_line_prefix(buf[i], length, offset + i * 4, quadlet[i], true);

        if (quadlet[i] > 0) {
            consumed += snprintf(buf[i] + consumed, length - consumed, "\"");

            for (j = 0; j < 4; ++j) {
                size_t shift = 24 - j * 8;
                uint8_t letter = (quadlet[i] >> shift) & 0xff;
                if (letter != '\0')
                    consumed += snprintf(buf[i] + consumed, length - consumed, "%c", letter);
            }

            snprintf(buf[i] + consumed, length - consumed, "\"");
        }
    }

    return i;
}

static size_t format_csr_icon_descriptor_leaf_content(char **buf, size_t length, size_t offset,
                                                      const uint32_t *quadlet, size_t quadlet_count,
                                                      const char *spec_name)
{
    int i;

    for (i = 0; i < quadlet_count; ++i)
        format_line_prefix(buf[i], length, offset + i * 4, quadlet[i], false);

    return i;
}

static size_t format_csr_unspecified_descriptor_leaf_content(char **buf, size_t length,
                                                             size_t offset, const uint32_t *quadlet,
                                                             size_t quadlet_count,
                                                             const char *spec_name)
{
    int i;

    for (i = 0; i < quadlet_count; ++i)
        format_line_prefix(buf[i], length, offset + i * 4, quadlet[i], false);

    return i;
}

#define CSR_DESC_TYPE_MASK    0xff000000
#define CSR_DESC_TYPE_SHIFT   24
#define CSR_SPEC_MASK         0x00ffffff
#define CSR_SPEC_SHIFT        0

#define CSR_DESC_TYPE_TEXTUAL 0x00
#define CSR_DESC_TYPE_ICON    0x01

static size_t format_csr_descriptor_leaf_content(char **buf, size_t length, size_t offset,
                                                 const uint32_t *quadlet, size_t quadlet_count,
                                                 const char *spec_name)
{
    size_t (*format)(char **buf, size_t length, size_t offset, const uint32_t *quadlet,
                     size_t quadlet_count, const char *spec_name);
    uint8_t desc_type;
    uint32_t spec_id;
    char desc_type_name[64];
    size_t consumed;
    size_t lines;

    if (quadlet_count < 1)
        return 0;

    desc_type = (quadlet[0] & CSR_DESC_TYPE_MASK) >> CSR_DESC_TYPE_SHIFT;
    spec_id = (quadlet[0] & CSR_SPEC_MASK) & CSR_SPEC_SHIFT;

    switch (desc_type) {
    case CSR_DESC_TYPE_TEXTUAL:
        snprintf(desc_type_name, sizeof(desc_type_name), "textual descriptor");
        format = format_csr_textual_descriptor_leaf_content;
        break;
    case CSR_DESC_TYPE_ICON:
        snprintf(desc_type_name, sizeof(desc_type_name), "icon descriptor");
        format = format_csr_icon_descriptor_leaf_content;
        break;
    default:
        snprintf(desc_type_name, sizeof(desc_type_name), "descriptor_type %02x, specifier_ID %x",
                 desc_type, spec_id);
        format = format_csr_unspecified_descriptor_leaf_content;
        break;
    }

    lines = 0;

    consumed = format_line_prefix(buf[lines], length, offset, quadlet[0], true);
    snprintf(buf[lines] + consumed, length - consumed, "%s", desc_type_name);
    ++lines;

    offset += 4;
    ++quadlet;
    quadlet_count -= 1;

    lines += format(buf + lines, length, offset, quadlet, quadlet_count, spec_name);

    return lines;
}

static size_t format_csr_keyword_leaf_content(char **buf, size_t length, size_t offset,
                                              const uint32_t *quadlet, size_t quadlet_count,
                                              const char *spec_name)
{
    int i;

    for (i = 0; i < quadlet_count; ++i) {
        size_t consumed;
        int j;

        consumed = format_line_prefix(buf[i], length, offset + 4 * i, quadlet[i], true);
        if (quadlet[i] > 0) {
            consumed += snprintf(buf[i] + consumed, length - consumed, "\"");

            for (j = 0; j < 4; ++j) {
                size_t shift = 24 - j * 8;
                uint8_t letter = (quadlet[i] >> shift) & 0xff;

                if (letter != '\0')
                    consumed += snprintf(buf[i] + consumed, length - consumed, "%c", letter);
                else if (i < quadlet_count - 1)
                    consumed += snprintf(buf[i] + consumed, length - consumed, "\" \"");
                else
                    break;
            }

            snprintf(buf[i] + consumed, length - consumed, "\"");
        }
    }

    return i;
}

static size_t format_csr_unit_location_leaf_content(char **buf, size_t length, size_t offset,
                                                    const uint32_t *quadlet, size_t quadlet_count,
                                                    const char *spec_name)
{
    uint64_t base_address;
    uint64_t upper_bound;
    size_t consumed;

    if (quadlet_count < 4)
        return 0;

    base_address = (((uint64_t)quadlet[0]) << 32) | quadlet[1];
    upper_bound = (((uint64_t)quadlet[2]) << 32) | quadlet[3];

    consumed = format_line_prefix(buf[0], length, offset, quadlet[0], true);
    snprintf(buf[0] + consumed, length - consumed, "base_address %016lx", base_address);

    format_line_prefix(buf[1], length, offset + 4, quadlet[1], false);

    consumed = format_line_prefix(buf[2], length, offset + 8, quadlet[2], true);
    snprintf(buf[2] + consumed, length - consumed, "upper_bound %016lx", upper_bound);

    format_line_prefix(buf[3], length, offset + 12, quadlet[3], false);

    return 4;
}

static size_t format_csr_eui64_leaf_content(char **buf, size_t length, size_t offset,
                                            const uint32_t *quadlet, size_t quadlet_count,
                                            const char *spec_name)
{
    uint32_t company_id;
    uint64_t device_id;
    uint64_t eui64;
    size_t consumed;

    if (quadlet_count < 2)
        return 0;

    company_id = (quadlet[0] & 0xffffff00) >> 8;
    device_id = ((((uint64_t)quadlet[0]) & 0x000000ff) << 32) | quadlet[1];
    eui64 = (((uint64_t)quadlet[0]) << 32) | quadlet[1];

    consumed = format_line_prefix(buf[0], length, offset, quadlet[0], true);
    snprintf(buf[0] + consumed, length - consumed, "company_id %06x     | ", company_id);

    consumed = format_line_prefix(buf[1], length, offset + 4, quadlet[1], true);
    snprintf(buf[1] + consumed, length - consumed, "device_id %010lx  | EUI-64 %016lx", device_id,
             eui64);

    return 2;
}

static size_t format_unspecified_leaf_content(char **buf, size_t length, size_t offset,
                                              const uint32_t *quadlet, size_t quadlet_count,
                                              const char *spec_name)
{
    int i;

    for (i = 0; i < quadlet_count; ++i)
        format_line_prefix(buf[i], length, offset + i * 4, quadlet[i], false);

    return i;
}

static const struct key_formatter csr_key_formatters[] = {
    {
        KEY_TYPE_LEAF,
        KEY_ID_CSR_DESCRIPTOR,
        CSR_DESCRIPTOR_NAME,
        .format_content.leaf = format_csr_descriptor_leaf_content,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_CSR_DESCRIPTOR,
        CSR_DESCRIPTOR_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_BUS_DEP_INFO,
        CSR_BUS_DEP_INFO_NAME,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_CSR_BUS_DEP_INFO,
        CSR_BUS_DEP_INFO_NAME,
        .format_content.leaf = format_unspecified_leaf_content,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_CSR_BUS_DEP_INFO,
        CSR_BUS_DEP_INFO_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_VENDOR_INFO,
        CSR_VENDOR_INFO_NAME,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_CSR_VENDOR_INFO,
        CSR_VENDOR_INFO_NAME,
        .format_content.leaf = format_unspecified_leaf_content,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_CSR_VENDOR_INFO,
        CSR_VENDOR_INFO_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_HARDWARE_VERSION,
        CSR_HARDWARE_VERSION_NAME,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_CSR_MODULE_INFO,
        CSR_MODULE_INFO_NAME,
        .format_content.leaf = format_csr_eui64_leaf_content,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_CSR_MODULE_INFO,
        CSR_MODULE_INFO_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_CSR_EUI_64,
        CSR_EUI_64_NAME,
        .format_content.leaf = format_csr_eui64_leaf_content,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_CSR_UNIT,
        CSR_UNIT_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_SPECIFIER_ID,
        CSR_SPECIFIER_ID_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_VERSION,
        CSR_VERSION_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_DEP_INFO,
        CSR_DEP_INFO_NAME,
    },
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_CSR_DEP_INFO,
        CSR_DEP_INFO_NAME,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_CSR_DEP_INFO,
        CSR_DEP_INFO_NAME,
        .format_content.leaf = format_unspecified_leaf_content,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_CSR_DEP_INFO,
        CSR_DEP_INFO_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_CSR_UNIT_LOCATION,
        CSR_UNIT_LOCATION_NAME,
        .format_content.leaf = format_csr_unit_location_leaf_content,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_MODEL,
        CSR_MODEL_NAME,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_CSR_INSTANCE,
        CSR_INSTANCE_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_CSR_KEYWORD,
        CSR_KEYWORD_NAME,
        .format_content.leaf = format_csr_keyword_leaf_content,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_CSR_FEATURE,
        CSR_FEATURE_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_DIRECTORY_ID,
        CSR_DIRECTORY_ID_NAME,
    },
};

////////////////////////////////////////////////
// Directory entries specific to IEEE 1394 bus.
////////////////////////////////////////////////

static size_t format_ieee1394_bus_node_capabilities_immediate_value(char *buf, size_t length,
                                                                    uint32_t value)
{
    return snprintf(buf, length, "per IEEE 1394");
}

static const struct key_formatter ieee1394_bus_key_formatters[] = {
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_CSR_NODE_CAPS,
        CSR_NODE_CAPS_NAME,
        .format_content.immediate = format_ieee1394_bus_node_capabilities_immediate_value,
    },
};

//////////////////////////////////////
// Directory entries specific to SBP.
//////////////////////////////////////

#define KEY_ID_SBP2_UNIT_UNIQUE_ID        0x0d // For leaf.
#define KEY_ID_SBP2_LOGICAL_UNIT_NUMBER   0x14 // For immediate.
#define KEY_ID_SBP2_MANAGEMENT_AGENT      0x14 // For CSR offset.
#define KEY_ID_SBP2_LOGICAL_UNIT          0x14 // For directory.
#define KEY_ID_SBP3_REVISION              0x21 // For immediate.
#define KEY_ID_SBP3_PLUG_CONTROL_REGISTER 0x32 // For immediate.
#define KEY_ID_SBP2_COMMAND_SET_SPEC_ID   0x38 // For immediate.
#define KEY_ID_SBP2_COMMAND_SET           0x39 // For immediate.
#define KEY_ID_SBP2_UNIT_CHARACTERISTIC   0x3a // For immediate.
#define KEY_ID_SBP2_COMMAND_SET_REVISION  0x3b // For immediate.
#define KEY_ID_SBP2_FIRMWARE_REVISION     0x3c // For immediate.
#define KEY_ID_SBP2_RECONNECT_TIMEOUT     0x3d // For immediate.
#define KEY_ID_SBP3_FAST_START            0x3e // For immediate.

#define SBP2_UNIT_UNIQUE_ID_NAME          "unit unique id"
#define SBP2_LOGICAL_UNIT_NUMBER_NAME     "logical unit number"
#define SBP2_MANAGEMENT_AGENT_NAME        "management agent CSR"
#define SBP2_LOGICAL_UNIT_NAME            "logical unit"
#define SBP3_REVISION_NAME                "revision"
#define SBP3_PLUG_CONTROL_REGISTER_NAME   "plug control register"
#define SBP2_COMMAND_SET_SPEC_ID_NAME     "command set spec id"
#define SBP2_COMMAND_SET_NAME             "command set"
#define SBP2_UNIT_CHARACTERISTIC_NAME     "unit char."
#define SBP2_COMMAND_SET_REVISION_NAME    "command set revision"
#define SBP2_FIRMWARE_REVISION_NAME       "firmware revision"
#define SBP2_RECONNECT_TIMEOUT_NAME       "reconnect timeout"
#define SBP3_FAST_START_NAME              "fast start"

static size_t format_sbp_logical_unit_number_immediate_value(char *buf, size_t length,
                                                             uint32_t value)
{
    static const char *const device_types[] = {
        [0x00] = "Disk",    [0x01] = "Tape",      [0x02] = "Printer",  [0x03] = "Processor",
        [0x04] = "WORM",    [0x05] = "CD/DVD",    [0x06] = "Scanner",  [0x07] = "MOD",
        [0x08] = "Changer", [0x09] = "Comm",      [0x0a] = "Prepress", [0x0b] = "Prepress",
        [0x0c] = "RAID",    [0x0d] = "Enclosure", [0x0e] = "RBC",      [0x0f] = "OCRW",
        [0x10] = "Bridge",  [0x11] = "OSD",       [0x12] = "ADC-2",
    };
    bool extended = (value & 0x800000) >> 23;
    unsigned int ordered = (value & 0x400000) >> 22;
    bool isoc = (value & 0x200000) >> 21;
    unsigned int device_type = (value & 0x1f) >> 16;
    unsigned int logical_unit = value & 0x00ffff;
    size_t consumed = 0;

    if (extended)
        consumed += snprintf(buf + consumed, length - consumed, " extended_status 1,");

    consumed += snprintf(buf + consumed, length - consumed, " ordered %d,", ordered);

    if (isoc)
        consumed += snprintf(buf + consumed, length - consumed, " isoch 1,");

    if (device_type < CONST_ARRAY_SIZE(device_types)) {
        consumed +=
            snprintf(buf + consumed, length - consumed, "type %s,", device_types[device_type]);
    } else if (logical_unit == 0x1e) {
        consumed += snprintf(buf + consumed, length - consumed, "type w.k.LUN,");
    } else if (logical_unit == 0x1f) {
        consumed += snprintf(buf + consumed, length - consumed, "type unknown,");
    } else {
        consumed += snprintf(buf + consumed, length - consumed, "type %02x?,", device_type);
    }

    return consumed;
}

static size_t format_sbp3_revision_immediate_value(char *buf, size_t length, uint32_t value)
{
    size_t consumed = snprintf(buf, length, "%d", value);

    switch (value) {
    case 0:
        consumed += snprintf(buf + consumed, length - consumed, " = SBP-2");
        break;
    case 1:
        consumed += snprintf(buf + consumed, length - consumed, " = SBP-3");
        break;
    default:
        break;
    }

    return consumed;
}

static size_t format_sbp3_plug_control_register_immediate_value(char *buf, size_t length,
                                                                uint32_t value)
{
    bool is_output = (value & 0x20) >> 5;
    unsigned int plug_index = value & 0x1f;

    return snprintf(buf, length, "plug control register: %sPCR, plug_index %d",
                    is_output ? "o" : "i", plug_index);
}

static size_t format_sbp_command_set_immediate_value(char *buf, size_t length, uint32_t value)
{
    if (value == 0x0104d8)
        return snprintf(buf, length, "SCSI Primary Commands 2 and related standards");
    else if (value == 0x010001)
        return snprintf(buf, length, "AV/C");
    else
        return 0;
}

static size_t format_sbp_unit_characteristic_immediate_value(char *buf, size_t length,
                                                             uint32_t value)
{
    bool distributed_data = (value & 0x010000) >> 16; // Extended by SBP-3.
    float mgt_orb_timeout_sec = 0.5 * ((value & 0x00ff00) >> 8);
    unsigned int orb_size = value & 0x0000ff;
    size_t consumed = 0;

    if (distributed_data)
        consumed += snprintf(buf + consumed, length - consumed, "distrib. data 1, ");

    consumed += snprintf(buf + consumed, length - consumed,
                         "mgt_ORB_timeout %gs, ORB_size %d quadlets", mgt_orb_timeout_sec,
                         orb_size);

    return consumed;
}

static size_t format_sbp_firmware_revision_immediate_value(char *buf, size_t length, uint32_t value)
{
    return snprintf(buf, length, "%06x", value);
}

static size_t format_sbp_reconnect_timeout_immediate_value(char *buf, size_t length, uint32_t value)
{
    unsigned int max_reconnect_hold = 1 + (value & 0x00ffff);

    return snprintf(buf, length, "reconnect timeout: max_reconnect_hold %ds", max_reconnect_hold);
}

static size_t format_sbp3_fast_start_immediate_value(char *buf, size_t length, uint32_t value)
{
    unsigned int max_payload = (value & 0x00ff00) >> 8;
    unsigned int fast_start_offset = value & 0x0000ff;
    size_t consumed = 0;

    if (max_payload > 0) {
        unsigned int max_payload_bytes = max_payload << 2;

        consumed += snprintf(buf + consumed, length - consumed, " max_payload %d bytes,",
                             max_payload_bytes);
    } else {
        consumed += snprintf(buf + consumed, length - consumed, " max_payload per max_rec,");
    }

    consumed += snprintf(buf + consumed, length - consumed, " offset %d", fast_start_offset);

    return consumed;
}

static const struct key_formatter incits_sbp_key_formatters[] = {
    {
        KEY_TYPE_LEAF,
        KEY_ID_SBP2_UNIT_UNIQUE_ID,
        SBP2_UNIT_UNIQUE_ID_NAME,
        .format_content.leaf = format_csr_eui64_leaf_content,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP2_LOGICAL_UNIT_NUMBER,
        SBP2_LOGICAL_UNIT_NUMBER_NAME,
        .format_content.immediate = format_sbp_logical_unit_number_immediate_value,
    },
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_SBP2_MANAGEMENT_AGENT,
        SBP2_MANAGEMENT_AGENT_NAME,
    },
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_SBP2_LOGICAL_UNIT,
        SBP2_LOGICAL_UNIT_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP3_REVISION,
        SBP3_REVISION_NAME,
        .format_content.immediate = format_sbp3_revision_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP3_PLUG_CONTROL_REGISTER,
        SBP3_PLUG_CONTROL_REGISTER_NAME,
        .format_content.immediate = format_sbp3_plug_control_register_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP2_COMMAND_SET_SPEC_ID,
        SBP2_COMMAND_SET_SPEC_ID_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP2_COMMAND_SET,
        SBP2_COMMAND_SET_NAME,
        .format_content.immediate = format_sbp_command_set_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP2_UNIT_CHARACTERISTIC,
        SBP2_UNIT_CHARACTERISTIC_NAME,
        .format_content.immediate = format_sbp_unit_characteristic_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP2_COMMAND_SET_REVISION,
        SBP2_COMMAND_SET_REVISION_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP2_FIRMWARE_REVISION,
        SBP2_FIRMWARE_REVISION_NAME,
        .format_content.immediate = format_sbp_firmware_revision_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP2_RECONNECT_TIMEOUT,
        SBP2_RECONNECT_TIMEOUT_NAME,
        .format_content.immediate = format_sbp_reconnect_timeout_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_SBP3_FAST_START,
        SBP3_FAST_START_NAME,
        .format_content.immediate = format_sbp3_fast_start_immediate_value,
    },
};

///////////////////////////////////////
// Directory entries specific to IIDC.
///////////////////////////////////////

#define KEY_ID_IIDC_CMD_REG_BASE             0x00 // For immediate.
#define KEY_ID_IIDC_VENDOR_NAME              0x01 // For leaf.
#define KEY_ID_IIDC_MODEL_NAME               0x02 // For leaf.

#define IIDC_CMD_REG_BASE_NAME               "command_regs_base"
#define IIDC_VENDOR_NAME                     "vendor name"
#define IIDC_MODEL_NAME                      "model name"

#define KEY_ID_IIDC_131_UNIT_SUB_SW_VERSION  0x38 // For immediate.
#define KEY_ID_IIDC_131_RESERVED_0           0x39 // For immediate.
#define KEY_ID_IIDC_131_RESERVED_1           0x3a // For immediate.
#define KEY_ID_IIDC_131_RESERVED_2           0x3b // For immediate.
#define KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_0 0x3c // For immediate.
#define KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_1 0x3d // For immediate.
#define KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_2 0x3e // For immediate.
#define KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_3 0x3f // For immediate.

#define IIDC_131_UNIT_SUB_SW_VERSION_NAME    "unit sub sw version"
#define IIDC_131_RESERVED_NAME               "(reserved)"
#define IIDC_131_VENDOR_UNIQUE_INFO_0_NAME   "vendor_unique_info_0"
#define IIDC_131_VENDOR_UNIQUE_INFO_1_NAME   "vendor_unique_info_1"
#define IIDC_131_VENDOR_UNIQUE_INFO_2_NAME   "vendor_unique_info_2"
#define IIDC_131_VENDOR_UNIQUE_INFO_3_NAME   "vendor_unique_info_3"

#define IIDC2_CMD_REG_BASE_NAME              "IIDC2Entry"

static size_t format_iidc_131_unit_sub_sw_version_immediate_value(char *buf, size_t length,
                                                                  uint32_t value)
{
    return snprintf(buf, length, "v1.3%d", value >> 4);
}

static size_t format_iidc2_100_unit_sub_sw_version_immediate_value(char *buf, size_t length,
                                                                   uint32_t value)
{
    unsigned int major = value >> 16;
    unsigned int minor = (value >> 8) & 0xff;
    unsigned int micro = value & 0xff;

    return snprintf(buf, length, "v%d.%d.%d", major, minor, micro);
}

static size_t format_iidc_104_leaf_content(char **buf, size_t length, size_t offset,
                                           const uint32_t *quadlet, size_t quadlet_count,
                                           const char *spec_name)
{
    int i;

    for (i = 0; i < 2; ++i)
        format_line_prefix(buf[i], length, offset + i * 4, quadlet[i], false);

    for (; i < quadlet_count; ++i) {
        size_t consumed;
        int j;

        consumed = format_line_prefix(buf[i], length, offset + i * 4, quadlet[i], true);
        if (quadlet[i] > 0) {
            consumed += snprintf(buf[i] + consumed, length - consumed, "\"");

            for (j = 0; j < 4; ++j) {
                size_t shift = 24 - j * 8;
                uint8_t letter = (quadlet[i] >> shift) & 0xff;
                if (letter != '\0')
                    consumed += snprintf(buf[i] + consumed, length - consumed, "%c", letter);
            }

            snprintf(buf[i] + consumed, length - consumed, "\"");
        }
    }

    return i;
}

static const struct key_formatter ta1394_iidc_104_key_formatters[] = {
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_IIDC_CMD_REG_BASE,
        IIDC_CMD_REG_BASE_NAME,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_IIDC_VENDOR_NAME,
        IIDC_VENDOR_NAME,
        .format_content.leaf = format_iidc_104_leaf_content,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_IIDC_MODEL_NAME,
        IIDC_MODEL_NAME,
        .format_content.leaf = format_iidc_104_leaf_content,
    },
};

static const struct key_formatter ta1394_iidc_131_key_formatters[] = {
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_IIDC_CMD_REG_BASE,
        IIDC_CMD_REG_BASE_NAME,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_IIDC_VENDOR_NAME,
        IIDC_VENDOR_NAME,
        .format_content.leaf = format_iidc_104_leaf_content,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_IIDC_MODEL_NAME,
        IIDC_MODEL_NAME,
        .format_content.leaf = format_iidc_104_leaf_content,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_UNIT_SUB_SW_VERSION,
        IIDC_131_UNIT_SUB_SW_VERSION_NAME,
        .format_content.immediate = format_iidc_131_unit_sub_sw_version_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_RESERVED_0,
        IIDC_131_RESERVED_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_RESERVED_1,
        IIDC_131_RESERVED_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_RESERVED_2,
        IIDC_131_RESERVED_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_0,
        IIDC_131_VENDOR_UNIQUE_INFO_0_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_1,
        IIDC_131_VENDOR_UNIQUE_INFO_1_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_2,
        IIDC_131_VENDOR_UNIQUE_INFO_2_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_3,
        IIDC_131_VENDOR_UNIQUE_INFO_3_NAME,
    },
};

static const struct key_formatter ta1394_iidc2_100_key_formatters[] = {
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_IIDC_CMD_REG_BASE,
        IIDC2_CMD_REG_BASE_NAME,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_IIDC_VENDOR_NAME,
        IIDC_VENDOR_NAME,
        .format_content.leaf = format_iidc_104_leaf_content,
    },
    {
        KEY_TYPE_LEAF,
        KEY_ID_IIDC_MODEL_NAME,
        IIDC_MODEL_NAME,
        .format_content.leaf = format_iidc_104_leaf_content,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_UNIT_SUB_SW_VERSION,
        IIDC_131_UNIT_SUB_SW_VERSION_NAME,
        .format_content.immediate = format_iidc2_100_unit_sub_sw_version_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_RESERVED_0,
        IIDC_131_RESERVED_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_RESERVED_1,
        IIDC_131_RESERVED_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_RESERVED_2,
        IIDC_131_RESERVED_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_0,
        IIDC_131_VENDOR_UNIQUE_INFO_0_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_1,
        IIDC_131_VENDOR_UNIQUE_INFO_1_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_2,
        IIDC_131_VENDOR_UNIQUE_INFO_2_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IIDC_131_VENDOR_UNIQUE_INFO_3,
        IIDC_131_VENDOR_UNIQUE_INFO_3_NAME,
    },
};

//////////////////////////////////////
// Directory entries specific to DPP.
//////////////////////////////////////

#define KEY_ID_DPP_111_COMMAND_SET_DIRECTORY      0x14 // Just for directory.
#define KEY_ID_DPP_111_COMMAND_SET_SPEC_ID        0x38 // Just for immediate.
#define KEY_ID_DPP_111_COMMAND_SET                0x39 // Just for immediate.
#define KEY_ID_DPP_111_COMMAND_SET_DETAILS        0x3a // Just for immediate.
#define KEY_ID_DPP_111_CONNECTION_REGISTER        0x3b // Just for CSR offset.
#define KEY_ID_DPP_111_WRITE_TRANSACTION_INTERVAL 0x3c // Just for immediate.
#define KEY_ID_DPP_111_UNIT_SW_DETAILS            0x3d // Just for immediate.

#define DPP_111_COMMAND_SET_DIRECTORY_NAME        "command set directory"
#define DPP_111_COMMAND_SET_SPEC_ID_NAME          "command set spec id"
#define DPP_111_COMMAND_SET_NAME                  "command set"
#define DPP_111_COMMAND_SET_DETAILS_NAME          "command set details"
#define DPP_111_CONNECTION_REGISTER_NAME          "connection CSR"
#define DPP_111_WRITE_TRANSACTION_INTERVAL_NAME   "write transaction interval"
#define DPP_111_UNIT_SW_DETAILS_NAME              "unit sw details"

static size_t format_dpp_111_command_set_immediate_value(char *buf, size_t length, uint32_t value)
{
    switch (value) {
    case 0xb081f2:
        return snprintf(buf, length, "DPC");
    case 0x020000:
        return snprintf(buf, length, "FTC");
    default:
        return 0;
    }
}

static size_t format_dpp_111_write_transaction_interval_immediate_value(char *buf, size_t length,
                                                                        uint32_t value)
{
    return snprintf(buf, length, "%dms", value);
}

static size_t format_dpp_111_unit_sw_details_immediate_value(char *buf, size_t length,
                                                             uint32_t value)
{
    unsigned int major = (value & 0x00f00000) >> 20;
    unsigned int minor = (value & 0x000f0000) >> 16;
    unsigned int micro = (value & 0x0000f000) >> 12;
    bool sdu_write_order = value & 1;

    return snprintf(buf, length, "v%d.%d.%d, sdu_write_order %d", major, minor, micro,
                    sdu_write_order);
}

static const struct key_formatter ta1394_dpp_111_key_formatters[] = {
    {
        KEY_TYPE_DIRECTORY,
        KEY_ID_DPP_111_COMMAND_SET_DIRECTORY,
        DPP_111_COMMAND_SET_DIRECTORY_NAME,
        .format_content.directory = format_directory_entries,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_DPP_111_COMMAND_SET_SPEC_ID,
        DPP_111_COMMAND_SET_SPEC_ID_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_DPP_111_COMMAND_SET,
        DPP_111_COMMAND_SET_NAME,
        .format_content.immediate = format_dpp_111_command_set_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_DPP_111_COMMAND_SET_DETAILS,
        DPP_111_COMMAND_SET_DETAILS_NAME,
    },
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_DPP_111_CONNECTION_REGISTER,
        DPP_111_CONNECTION_REGISTER_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_DPP_111_WRITE_TRANSACTION_INTERVAL,
        DPP_111_WRITE_TRANSACTION_INTERVAL_NAME,
        .format_content.immediate = format_dpp_111_write_transaction_interval_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_DPP_111_UNIT_SW_DETAILS,
        DPP_111_UNIT_SW_DETAILS_NAME,
        .format_content.immediate = format_dpp_111_unit_sw_details_immediate_value,
    },
};

///////////////////////////////////////
// Directory entries specific to IICP.
///////////////////////////////////////

#define KEY_ID_IICP_DETAILS                     0x38 // For immediate.
#define KEY_ID_IICP_COMMAND_SET_SPEC_ID         0x39 // For immediate.
#define KEY_ID_IICP_COMMAND_SET                 0x3a // For immediate.
#define KEY_ID_IICP_COMMAND_SET_DETAILS         0x3b // For immediate.
#define KEY_ID_IICP_CONNECTION_REG_OFFSET       0x3c // For CSR offset.
#define KEY_ID_IICP_CAPABILITIES                0x3d // For immediate.
#define KEY_ID_IICP_INTERRUPT_ENABLE_REG_OFFSET 0x3e // For CSR offset.
#define KEY_ID_IICP_INTERRUPT_HANDR_REG_OFFSET  0x3f // For CSR offset.

#define IICP_DETAILS_NAME                       "details"
#define IICP_COMMAND_SET_SPEC_ID_NAME           "command set spec id"
#define IICP_COMMAND_SET_NAME                   "command set"
#define IICP_COMMAND_SET_DETAILS_NAME           "command set details"
#define IICP_CONNECTION_REG_OFFSET_NAME         "connection CSR"
#define IICP_CAPABILITIES_NAME                  "capabilities"
#define IICP_INTERRUPT_ENABLE_REG_OFFSET_NAME   "interrupt_enable CSR"
#define IICP_INTERRUPT_HANDR_REG_OFFSET_NAME    "interrupt_handlr CSR"

static size_t format_iicp_details_immediate_value(char *buf, size_t length, uint32_t value)
{
    unsigned int major =
        (((value & 0xf00000) >> 20) & 0xf) * 10 + (((value & 0x0f0000) >> 16) & 0xf);
    unsigned int minor =
        (((value & 0x00f000) >> 12) & 0xf) * 10 + (((value & 0x000f00) >> 8) & 0xf);

    return snprintf(buf, length, "v%d.%d", major, minor);
}

static size_t format_iicp_command_set_immediate_value(char *buf, size_t length, uint32_t value)
{
    switch (value) {
    case 0x4b661f:
        return snprintf(buf, length, "IICP only");
    case 0xc27f10:
        return snprintf(buf, length, "IICP488");
    default:
        return 0;
    }
}

static size_t format_iicp_command_set_details_immediate_value(char *buf, size_t length,
                                                              uint32_t value)
{
    unsigned int major =
        (((value & 0xf00000) >> 20) & 0xf) * 10 + (((value & 0x0f0000) >> 16) & 0xf);
    unsigned int minor =
        (((value & 0x00f000) >> 12) & 0xf) * 10 + (((value & 0x000f00) >> 8) & 0xf);

    return snprintf(buf, length, "v%d.%d", major, minor);
}

static size_t format_iicp_capabilities_immediate_value(char *buf, size_t length, uint32_t value)
{
    unsigned int reserved_high_proto = (value & 0xff0000) >> 16;
    unsigned int reserved_iicp = (value & 0x00ffc0) >> 6;
    unsigned int ccli = (value & 0x000020) >> 5;
    unsigned int cmgr = (value & 0x000010) >> 4;
    unsigned int max_int_length_exponent = (value & 0x00000f);
    size_t consumed = 0;

    consumed += snprintf(buf, length, "hi proto %d, IICP %d, ccli %d, cmgr %d", reserved_high_proto,
                         reserved_iicp, ccli, cmgr);

    if (max_int_length_exponent > 0) {
        unsigned int max_int_bytes = 2 << max_int_length_exponent;

        consumed +=
            snprintf(buf + consumed, length - consumed, "  maxIntLength %d bytes", max_int_bytes);
    } else {
        consumed += snprintf(buf + consumed, length - consumed, "  maxIntLength -");
    }

    return consumed;
}

static const struct key_formatter ta1394_iicp_key_formatters[] = {
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IICP_DETAILS,
        IICP_DETAILS_NAME,
        .format_content.immediate = format_iicp_details_immediate_value,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IICP_COMMAND_SET_SPEC_ID,
        IICP_COMMAND_SET_SPEC_ID_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IICP_COMMAND_SET,
        IICP_COMMAND_SET_NAME,
        .format_content.immediate = format_iicp_command_set_immediate_value,
    },
    { KEY_TYPE_IMMEDIATE, KEY_ID_IICP_COMMAND_SET_DETAILS, IICP_COMMAND_SET_DETAILS_NAME,
      .format_content.immediate = format_iicp_command_set_details_immediate_value },
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_IICP_CONNECTION_REG_OFFSET,
        IICP_CONNECTION_REG_OFFSET_NAME,
    },
    {
        KEY_TYPE_IMMEDIATE,
        KEY_ID_IICP_CAPABILITIES,
        IICP_CAPABILITIES_NAME,
        .format_content.immediate = format_iicp_capabilities_immediate_value,
    },
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_IICP_INTERRUPT_ENABLE_REG_OFFSET,
        IICP_INTERRUPT_ENABLE_REG_OFFSET_NAME,
    },
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_IICP_INTERRUPT_HANDR_REG_OFFSET,
        IICP_INTERRUPT_HANDR_REG_OFFSET_NAME,
    }
};

#define KEY_ID_APPLE_ISIGHT_AUDIO_REG 0x00 // For CSR offset.

#define APPLE_ISIGHT_AUDIO_REG_NAME   "register file"

static const struct key_formatter apple_isight_audio_key_formatters[] = {
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_APPLE_ISIGHT_AUDIO_REG,
        APPLE_ISIGHT_AUDIO_REG_NAME,
    },
};

#define KEY_ID_APPLE_ISIGHT_IRIS_REG 0x00 // For CSR offset.

#define APPLE_ISIGHT_IRIS_REG_NAME   "Iris Status Address register"

static const struct key_formatter apple_isight_iris_key_formatters[] = {
    {
        KEY_TYPE_CSR_OFFSET,
        KEY_ID_APPLE_ISIGHT_IRIS_REG,
        APPLE_ISIGHT_IRIS_REG_NAME,
    },
};

static size_t format_unspecified_immediate_value(char *buf, size_t length, uint32_t value)
{
    return snprintf(buf, length, "(immediate value)");
}

static const struct key_formatter *find_formatter(const struct key_formatter *formatters,
                                                  size_t formatter_count, uint32_t key_type,
                                                  uint32_t key_id)
{
    int i;

    for (i = 0; i < formatter_count; ++i) {
        const struct key_formatter *formatter = formatters + i;
        if (formatter->key_type == key_type && formatter->key_id == key_id)
            return formatter;
    }

    return NULL;
}

static void detect_key_formatter(const struct key_formatter **formatter, const char **spec_name,
                                 const struct spec_identifier *identifier, uint32_t key_type,
                                 uint32_t key_id)
{
    static const struct {
        const char *spec_name;
        const struct spec_identifier *identifier;
        const struct key_formatter *formatters;
        size_t formatter_count;
    } *spec_entry, spec_entries[] = {
        {
            SPEC_NAME_RFC_2734,
            &spec_iana_ipv4,
            NULL,
            0,
        },
        {
            SPEC_NAME_RFC_3146,
            &spec_iana_ipv6,
            NULL,
            0,
        },
        // NOTE: both SBP-2 and -3 use the same identifiers.
        {
            SPEC_NAME_SBP,
            &spec_incits_sbp,
            incits_sbp_key_formatters,
            CONST_ARRAY_SIZE(incits_sbp_key_formatters),
        },
        {
            SPEC_NAME_SBP_AVC,
            &spec_incits_sbp_avc,
            incits_sbp_key_formatters,
            CONST_ARRAY_SIZE(incits_sbp_key_formatters),
        },
        {
            SPEC_NAME_AVC,
            &spec_1394ta_avc,
            NULL,
            0,
        },
        {
            SPEC_NAME_CAL,
            &spec_1394ta_cal,
            NULL,
            0,
        },
        {
            SPEC_NAME_EHS,
            &spec_1394ta_ehs,
            NULL,
            0,
        },
        {
            SPEC_NAME_HAVI,
            &spec_1394ta_havi,
            NULL,
            0,
        },
        {
            SPEC_NAME_VENDOR_UNIQUE,
            &spec_1394ta_vendor_unique,
            NULL,
            0,
        },
        {
            SPEC_NAME_VENDOR_UNIQUE_AVC,
            &spec_1394ta_vendor_unique_avc,
            NULL,
            0,
        },
        {
            SPEC_NAME_IIDC_104,
            &spec_1394ta_iidc_104,
            ta1394_iidc_104_key_formatters,
            CONST_ARRAY_SIZE(ta1394_iidc_104_key_formatters),
        },
        {
            SPEC_NAME_IIDC_120,
            &spec_1394ta_iidc_120,
            ta1394_iidc_104_key_formatters,
            CONST_ARRAY_SIZE(ta1394_iidc_104_key_formatters),
        },
        {
            SPEC_NAME_IIDC_130,
            &spec_1394ta_iidc_130,
            ta1394_iidc_131_key_formatters,
            CONST_ARRAY_SIZE(ta1394_iidc_131_key_formatters),
        },
        {
            SPEC_NAME_IIDC2,
            &spec_1394ta_iidc2,
            ta1394_iidc2_100_key_formatters,
            CONST_ARRAY_SIZE(ta1394_iidc2_100_key_formatters),
        },
        {
            SPEC_NAME_DPP_111,
            &spec_1394ta_dpp_111,
            ta1394_dpp_111_key_formatters,
            CONST_ARRAY_SIZE(ta1394_dpp_111_key_formatters),
        },
        {
            SPEC_NAME_IICP,
            &spec_1394ta_iicp,
            ta1394_iicp_key_formatters,
            CONST_ARRAY_SIZE(ta1394_iicp_key_formatters),
        },
        {
            SPEC_NAME_ALESIS_AUDIO,
            &spec_alesis_audio,
            NULL,
            0,
        },
        {
            SPEC_NAME_ISIGHT_AUDIO,
            &spec_apple_isight_audio,
            apple_isight_audio_key_formatters,
            CONST_ARRAY_SIZE(apple_isight_audio_key_formatters),
        },
        {
            SPEC_NAME_ISIGHT_FACTORY,
            &spec_apple_isight_factory,
            NULL,
            0,
        },
        {
            SPEC_NAME_ISIGHT_IRIS,
            &spec_apple_isight_iris,
            apple_isight_iris_key_formatters,
            CONST_ARRAY_SIZE(apple_isight_iris_key_formatters),
        },
        {
            SPEC_NAME_LACIE_HID,
            &spec_lacie_hid,
            NULL,
            0,
        },
    };
    static const struct key_formatter default_formatters[] = {
        [KEY_TYPE_IMMEDIATE] =
            {
                KEY_TYPE_IMMEDIATE,
                INVALID_KEY_ID,
                UNSPECIFIED_ENTRY_NAME,
                .format_content.immediate = format_unspecified_immediate_value,
            },
        [KEY_TYPE_CSR_OFFSET] =
            {
                KEY_TYPE_CSR_OFFSET,
                INVALID_KEY_ID,
                UNSPECIFIED_ENTRY_NAME,
            },
        [KEY_TYPE_LEAF] =
            {
                KEY_TYPE_LEAF,
                INVALID_KEY_ID,
                UNSPECIFIED_ENTRY_NAME,
                .format_content.leaf = format_unspecified_leaf_content,
            },
        [KEY_TYPE_DIRECTORY] =
            {
                KEY_TYPE_DIRECTORY,
                INVALID_KEY_ID,
                UNSPECIFIED_ENTRY_NAME,
                .format_content.directory = format_directory_entries,
            },
    };
    int i;

    spec_entry = NULL;
    for (i = 0; i < CONST_ARRAY_SIZE(spec_entries); ++i) {
        if (!memcmp(spec_entries[i].identifier, identifier, sizeof(*identifier))) {
            spec_entry = spec_entries + i;
            break;
        }
    }

    if (spec_entry != NULL) {
        *formatter =
            find_formatter(spec_entry->formatters, spec_entry->formatter_count, key_type, key_id);
        if (*formatter != NULL) {
            *spec_name = spec_entry->spec_name;
            return;
        }
    }

    *formatter = find_formatter(ieee1394_bus_key_formatters,
                                CONST_ARRAY_SIZE(ieee1394_bus_key_formatters), key_type, key_id);
    if (*formatter != NULL)
        return;

    *formatter =
        find_formatter(csr_key_formatters, CONST_ARRAY_SIZE(csr_key_formatters), key_type, key_id);
    if (*formatter != NULL)
        return;

    *formatter = &default_formatters[key_type];
}