File: iucode_tool.c

package info (click to toggle)
iucode-tool 2.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 860 kB
  • sloc: sh: 4,178; ansic: 3,155; makefile: 23
file content (3808 lines) | stat: -rw-r--r-- 97,378 bytes parent folder | download | duplicates (2)
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
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
/*
 * iucode_tool - Manipulates Intel(R) IA32/x86_64 processor microcode bundles
 *
 * Copyright (c) 2010-2018 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
 *               2000 Simon Trimmer, Tigran Aivazian
 *
 * Some code copied from microcode.ctl v1.17.
 * Some code based on the Linux kernel microcode_intel driver.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 */

#include "iucode_tool_config.h"

#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <argp.h>
#include <dirent.h>
#include <time.h>
#include <cpuid.h>

#include "intel_microcode.h"

#define PROGNAME "iucode_tool"

/*
 * For micro-optimization on the hotter paths
 */
#define likely(x)       __builtin_expect(!!(x), 1)
#define unlikely(x)     __builtin_expect(!!(x), 0)

/* cpio archive constants */
#define LINUX_CPIO_UCODE_NAME    "kernel/x86/microcode/GenuineIntel.bin"
#define LINUX_CPIO_UCODE_PADNAME "kernel/x86/microcode/.enuineIntel.align.0123456789abcdef"
#define LINUX_CPIO_FILE_MODE 0100644U
#define LINUX_CPIO_DIR_MODE  0040755U
#define LINUX_CPIO_BLK_SIZE  1024U

/* file load constants */
#define IUCODE_TYPICAL_MCU_FILE_SIZE (0x200000UL) /* 2MiB */
#define IUCODE_MAX_MCU_FILE_SIZE  (0x40000000ULL) /* 1GiB, arbitrary */

#define IUCODE_DAT_FAST_EXMAX  10    /* Fast-path disable threshold */
#define IUCODE_DAT_LBUF_SIZE 2048    /* Maximum line length for .dat */

/* Command-line processing and UI */

enum {
	EXIT_USAGE = 1,		/* Exit due to command line errors */
	EXIT_SWFAILURE = 2,	/* Exit due to software failure (ENOMEM, etc) */
};

static enum { /* actions */
	IUCODE_DO_UPLOADUC = 0x0001,
	IUCODE_DO_WRITEUC  = 0x0002,
	IUCODE_DO_WRITEFW  = 0x0004,
	IUCODE_DO_SELPROC  = 0x0008,
	IUCODE_DO_LOADFILE = 0x0010,
	IUCODE_DO_WRITEFWN = 0x0020,
	IUCODE_DO_WRITEFWE = 0x0040,
	IUCODE_DO_WRITFWNA = 0x0080,

	IUCODE_F_UCSELECT  = 0x1000,

	IUCODE_DOMASK_NEEDSUC = IUCODE_DO_UPLOADUC
				| IUCODE_DO_WRITEUC
				| IUCODE_DO_WRITEFW
				| IUCODE_DO_WRITEFWN
				| IUCODE_DO_WRITEFWE
				| IUCODE_DO_WRITFWNA,
} command_line_actions;

static char *progname;
static int verbosity = 1; /* 0 = errors, 1 = normal... */
static int strict_checks = 1;
static int ignore_bad_ucode = 0;
static int allow_downgrade = 0;
static int list_all_microcodes = 0;
static int list_sel_microcodes = 0;
static int unlink_files = 0;
static char *upload_microcode = NULL;
static char *write_microcode = NULL;
static char *write_early_firmware = NULL;
static char *write_firmware = NULL;
static char *write_named = NULL;
static char *write_named_all = NULL;

typedef enum { /* File type */
	INTEL_UC_FT_UNKNOWN = 0,
	INTEL_UC_FT_DAT,
	INTEL_UC_FT_BIN,
	INTEL_UC_FT_SCAN,
} intel_ucode_file_type_t;
static intel_ucode_file_type_t ucfiletype = INTEL_UC_FT_UNKNOWN;

/* linked list of filenames */
struct filename_list {
	struct filename_list *next;
	intel_ucode_file_type_t type;
	char path[];
};
static struct filename_list *input_files = NULL;
static int processed_stdin = 0;

/* DAT parser */
static int fast_parser_enabled = 1;
static unsigned int fast_parser_exceptions = 0;

/* cpio generation - defaults must match
 * IUCODE_ARGP_DFLSIZE_EIRFS behavior */
static size_t cpio_blocksize = LINUX_CPIO_BLK_SIZE;
static int cpio_parentdirs = 1;

/* Intel Microcode data structures */

struct microcode_bundle {
	struct microcode_bundle *next;
	const char *filename;	/* source file name */
	unsigned int id;        /* bundle id */
	unsigned long int size; /* bytes */
	const void *data;       /* binary file data */
};
static struct microcode_bundle *microcode_bundles = NULL;
static struct microcode_bundle **microcode_bundles_tail = &microcode_bundles;
static unsigned int next_bundle_id = 1;

/* intel_uclist_entry signature flag constants */
#define INTEL_UCLE_EXTSIG 0x0001U /* Sig came from extended sig table */
#define INTEL_UCLE_HASXST 0x0002U /* ucode has an extended sig table */
#define INTEL_UCLE_SELID  0x0010U /* Sig is candidate for loose datefilter */
#define INTEL_UCLE_SELECT 0x0020U /* Sig selected */
#define INTEL_UCLE_NOWR   0x0100U /* Do not write this sig to output
                                   *  + When INTEL_UCLE_HASXST is set,
                                   *    INTEL_UCLE_NOWR is reserved for
                                   *    uclist_annotate_extsig_dup() */
#define INTEL_UCLE_DUPSIG 0x0200U /* Duplicate signature.  The first one
                                   * in load order won't have this flag */

/* signature single-linked list */
struct intel_uclist_entry {
	struct intel_uclist_entry *next;
	uint32_t cpuid;		/* sorting key */
	uint32_t pfm;		/* microcode platform ID (MSR 0x17) mask */
	int32_t  uc_rev;	/* duplicated from header */
	uint32_t flags;		/* INTEL_UCLE_ flags */
	unsigned int id;	/* microcode id within group */
	unsigned int gid;	/* microcode group id */
	const void *uc;
	uint32_t uc_size;
};
static struct intel_uclist_entry *all_microcodes = NULL;
static struct intel_uclist_entry *microcodes = NULL;

/* UI helpers */
#define UCODE_ID_FMT_UU "%03u/%03u"
#define UCODE_ID_MAX_LEN 22
struct microcode_iterator_data {
	const struct microcode_bundle *current_bundle;
	unsigned long int total_signature_count;
	unsigned long int total_unique_sig_count;
	unsigned long int total_entry_count;
	unsigned int current_uc;
};
static struct microcode_iterator_data microcode_iterator_data;

/* Filter masks */
#define IUCODE_FILTERMASK_SCANCPUS 0xffffffffU

enum iuc_rev_match_mode {
	IUCODE_REVFLT_ANY = 0,	/* Ignore revision on match */
	IUCODE_REVFLT_EQ,	/* Revision must be = filter's rev */
	IUCODE_REVFLT_LT,   	/* Revision must be < filter's rev */
	IUCODE_REVFLT_GT,	/* Revision must be > filter's rev */

	IUCODE_REVFLT_SIZE	/* EOL */
};
static const char * const iuc_rev_match_mode_s[IUCODE_REVFLT_SIZE] = {
	[IUCODE_REVFLT_ANY] = NULL,
	[IUCODE_REVFLT_EQ] = "eq:",
	[IUCODE_REVFLT_LT] = "lt:",
	[IUCODE_REVFLT_GT] = "gt:",
}; /* keep this in sync with enum iuc_rev_match_mode !! */

struct microcode_filter_entry {
	struct microcode_filter_entry *next;
	uint32_t cpuid;		/* exact match */
	uint32_t pfm;		/* common bits set match */
	int32_t  rev;
	enum iuc_rev_match_mode rev_match;
	int      invert;
};
static struct microcode_filter_entry *uc_filter_queue = NULL;
static struct microcode_filter_entry *uc_filter_queue_tail = NULL;
static struct microcode_filter_entry *uc_filter_list = NULL;
static int filter_list_allow = 1;

static uint32_t datefilter_max = 0xffffffffU;  /* select dates before this */
static uint32_t datefilter_min = 0;	       /* select dates after this */
static int datefilter_loose = 0;

static inline int filter_list_active(void)
{
	return !!uc_filter_list ||
		!!(command_line_actions & IUCODE_F_UCSELECT);
}

/* extended sig dedup */
struct microcode_id_entry {
	const void * id;	/* exact match */
	struct microcode_id_entry *next;
};
static unsigned int extsig_tables_in_use = 0;

/* Helpers */

#define print_msg_u(format, arg...) \
	do { fflush(stdout); fprintf(stderr, "%s: " format "\n", progname, ## arg); } while (0)

#define print_msg(level, format, arg...) \
	do { \
	    if (verbosity >= level) { \
		fflush(stdout); \
		fprintf(stderr, "%s: " format "\n", progname, ## arg); \
	    } \
	} while (0)

#define print_err(format, arg...) \
	do { fflush(stdout); fprintf(stderr, "%s: " format "\n", progname, ## arg); } while (0)

#define print_warn(format, arg...) \
	do { fflush(stdout); fprintf(stderr, "%s: warning: " format "\n", progname, ## arg); } while (0)

static inline int is_dash(const char * const fn)
{
	return fn && *fn == '-' && !*(fn+1);
}

static int parse_u32(const char *nptr, char **endptr, int base,
		     uint32_t * const res)
{
	unsigned long int ul;

	assert(nptr);
	assert(endptr);
	assert(res);

	errno = 0;
	ul = strtoul(nptr, endptr, base);
	if (errno || nptr == *endptr)
		return errno ? errno : EINVAL;
	if (ul > UINT32_MAX)
		return ERANGE;

	*res = (uint32_t)ul;
	return 0;
}

/* Do the right thing on ILP32... */
#if ULONG_MAX > UINT32_MAX
typedef long int l_int_t;
#define strtol_l strtol
#else
typedef long long int l_int_t;
#define strtol_l strtoll
#endif

static int parse_s32e(const char *nptr, char **endptr, int base,
		      int32_t * const res)
{
	l_int_t l; /* ILP32 requires long long */

	assert(nptr);
	assert(endptr);
	assert(res);

	errno = 0;
	l = strtol_l(nptr, endptr, base); /* strotl or strotll */
	if (errno || nptr == *endptr)
		return errno ? errno : EINVAL;
	if (l > UINT32_MAX || l < INT32_MIN)
		return ERANGE;

	/*
	 * Accept UINT32_MAX >= x > INT32_MAX, as if it were
	 * the binary representation of int32_t read as uint32_t
	 */
	*res = (int32_t)(l & UINT32_MAX);

	return 0;
}

#undef strtol_l

static int is_valid_fd(const int fd)
{
	return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}

static void fix_fds(const int fd, const int fl)
{
	int nfd;

	if (is_valid_fd(fd))
		return;

	nfd = open("/dev/null", fl);
	if (nfd == -1 || dup2(nfd, fd) == -1) {
		print_err("could not attach /dev/null to file descriptor %d: %s",
			  fd, strerror(errno));
		/* if (nfd != -1) close(nfd); - disabled as we're going to exit() now */
		exit(EXIT_SWFAILURE);
	}
	if (nfd != fd)
		close(nfd);
}

/*
 * glibc does not ensure sanity of the standard streams at program start
 * for non suid/sgid applications.  The streams are initialized as open
 * and not in an error state even when their underlying FDs are invalid
 * (closed).  These FDs will later become valid due to an unrelated
 * open(), which will cause undesired behavior (such as data corruption)
 * should the stream be used.
 *
 * freopen() cannot be used to fix this directly, due to a glibc 2.14+ bug
 * when freopen() is called on an open stream that has an invalid FD which
 * also happens to be the first available FD.
 */
static void sanitize_std_fds(void)
{
	/* do it in file descriptor numerical order! */
	fix_fds(STDIN_FILENO,  O_RDONLY);
	fix_fds(STDOUT_FILENO, O_WRONLY);
	fix_fds(STDERR_FILENO, O_RDWR);
}

/* Signature linked list */

/**
 * free_uclist() - frees chain of struct intel_uclist_entry
 * @p:		pointer to the first struct intel_uclist_entry
 *
 * If p != NULL, frees all members of the struct intel_uclist_entry
 * linked list.
 */
static void free_uclist(struct intel_uclist_entry *p)
{
	struct intel_uclist_entry *e;

	while (p) {
		e = p;
		p = e->next;
		free(e);
	}
}

/**
 * uclist_add_signature() - add signature to ucode sig list
 *
 * @id:		microcode id
 * @gid:	microcode group id
 * @flags:	INTEL_UCLE_* flags
 * @cpuid:	signature of the signature entry being added
 * @pf_mask:	pf_mask of the signature entry being added
 * @uc_rev:	revision of the microcode entry being added
 * @uc:		microcode data (including headers)
 * @uc_size:	microcode data size (total, including headers)
 * @strict:	validate microcode opaque data against duplicates
 * @uclist:	pointer to the first entry on the list
 *
 * Adds a microcode signature entry to the list.  Does not filter
 * out duplicates, nor superseed entries.
 *
 * If @strict is true, it will compare the opaque data with one
 * of the previously inserted duplicates (and return EBADF if it
 * doesn't match).
 *
 * returns: ENOMEM: could not allocate memory,
 *          EINVAL: bad @cpuid, @uc pointer or @uclist pointer
 *          EEXIST: entry already in list (will be inserted)
 *          EBADF : entry already in list, with different opaque data
 *          0:      entry not yet in list (will be inserted)
 */
static int uclist_add_signature(const unsigned int id,
			const unsigned int gid,
			uint32_t flags,
			const uint32_t cpuid,
			const uint32_t pf_mask,
			const int32_t uc_rev,
			const void * const uc,
			const uint32_t uc_size,
			const int strict,
			struct intel_uclist_entry ** const uclist)
{
	struct intel_uclist_entry **pnext, *n, *d;
	int res = 0;

	if (unlikely(!cpuid || !uc || !uclist))
		return EINVAL;

	pnext = uclist;
	d = NULL;

	flags &= ~INTEL_UCLE_DUPSIG;

	/* search linked list for first insertion point */
	while (likely(*pnext && (*pnext)->cpuid < cpuid))
		pnext = &((*pnext)->next);

	/* look for duplicates, stop on the first */
	while (likely(*pnext) && (*pnext)->cpuid == cpuid) {
		if ((*pnext)->uc_rev == uc_rev) {
			if ((*pnext)->pfm == pf_mask) {
				res = EEXIST;
				flags |= INTEL_UCLE_DUPSIG;
				d = *pnext;
				break;
			} else if (((*pnext)->pfm & pf_mask)) {
				/* not a DUPSIG, but we need to
				 * compare the payloads */
				d = *pnext;
			}
		}
		pnext = &((*pnext)->next);
	}

	if (strict && d) {
		int rd = intel_ucode_compare(uc, d->uc);
		if (rd == -EINVAL || rd == -EBADF)
			return -rd;
	}

	n = malloc(sizeof(struct intel_uclist_entry));
	if (unlikely(!n))
		return ENOMEM;

	memset(n, 0, sizeof(struct intel_uclist_entry));
	n->id = id;
	n->gid = gid;
	n->cpuid = cpuid;
	n->pfm = pf_mask;
	n->uc_rev = uc_rev;
	n->uc = uc;
	n->uc_size = uc_size;
	n->flags = flags;

	/* prepend */
	n->next = *pnext;
	*pnext = n;

	return res;
}

/**
 * uclist_merge_signature() - merge signature into ucode sig list
 * @id:		microcode id
 * @gid:	microcode group id
 * @flags:	INTEL_UCLE_* flags
 * @cpuid:	signature of the signature entry being added
 * @pf_mask:	pf_mask of the signature entry being added
 * @uc_rev:	revision of the microcode entry being added
 * @uc:		microcode data (including headers)
 * @uc_size:	microcode data size (total, including headers)
 * @downgrade:	version downgrades between groups allowed if NZ
 * @uclist:	pointer to the first entry on the list
 *
 * Adds a microcode signature entry to the list.  When @downgrade
 * is zero, it will replace any suitable entry that has a lesser
 * revision.  When @downgrade is non-zero, it will replace any
 * suitable entry in the same microcode group that has a lesser
 * revision, or any suitable entry from a smaller (earlier)
 * microcode group regardless of revision.
 *
 * "suitable" means microcodes with the same cpuid and with a
 * pf_mask that is either equal or a strict subset of the one from
 * the new microcode.
 *
 * Note that it IS still possible that two signatures in the list
 * could apply to the same processor, if they have different revisions
 * and different pf masks, and the CPU is present in both pf masks.
 *
 * Note that downgrade mode is severely limited by the fact that we
 * do not change @pf_mask, and thus will often result in several
 * microcodes for the same processor being left in @uclist.
 *
 * returns: ENOMEM: could not allocate memory,
 *          EEXIST: entry already in list (but may have been
 *                  inserted if @downgrade is non-zero), or
 *                  contained by an entry already in the list
 *          EINVAL: bad @cpuid, @uc pointer or @uclist pointer
 */
static int uclist_merge_signature(const unsigned int id,
			const unsigned int gid,
			uint32_t flags,
			const uint32_t cpuid,
			const uint32_t pf_mask,
			const int32_t uc_rev,
			const void * const uc,
			const uint32_t uc_size,
			const int downgrade,
			struct intel_uclist_entry ** const uclist)
{
	struct intel_uclist_entry **pnext, **p_ins;
	enum { UCLM_INSERT, UCLM_DISCARD, UCLM_REPLACE } action = UCLM_INSERT;
	int res = 0;

	if (unlikely(!cpuid || !uc || !uclist))
		return EINVAL;

	flags &= ~INTEL_UCLE_DUPSIG;

	pnext = uclist;

	/*
	 * notes: pf_mask *can* be zero on old processors;
	 *        pf_masks rarely change: replace-in-place is typical;
	 */

	/* search linked list for matching CPUID */
	while (*pnext && (*pnext)->cpuid < cpuid)
		pnext = &((*pnext)->next);

	p_ins = NULL;

	/* first stage: look for replace/discard */
	while (*pnext && (*pnext)->cpuid == cpuid) {
		struct intel_uclist_entry * const e = *pnext;

		/* potential insertion point: sort by pf_mask, descending */
		if (!p_ins && pf_mask >= e->pfm)
			p_ins = pnext;

		if (pf_mask && (e->pfm & pf_mask) == 0) {
			/* disjoint sets, continue search */
			pnext = &(e->next);
			continue;
		}

		if (!downgrade || gid == e->gid) { /* do not downgrade */
			if ((e->pfm & pf_mask) == pf_mask) {
				/* new set same or contained in the old set */
				if (e->uc_rev >= uc_rev) {
					/* old set as good, or newer: discard new */
					action = UCLM_DISCARD;
					break;
				} else if (pf_mask == e->pfm) {
					/* the sets are identical, and the new
					 * one is newer microcode: replace old */
					action = UCLM_REPLACE;
					break;
				}
			} else if ((pf_mask & e->pfm) == e->pfm) {
				/* new set a superset of the old */
				if (uc_rev >= e->uc_rev) {
					/* we can replace the old entry */
					action = UCLM_REPLACE;
					break;
				}
			}
		} else if (gid > e->gid) { /* downgrade */
			/* FIXME?: we cannot handle downgrades where we'd have
			 * to remove bits from the pfmask of an entry to make
			 * it "stick".  We don't even attempt to handle
			 * these cases: we just insert.  This can be detected
			 * by a later pass over the entire @uclist for fixup or
			 * warnings. */
			if ((pf_mask & e->pfm) == e->pfm) {
				/* new set same, or superset of old: replace */
				res = (e->uc_rev == uc_rev && e->pfm == pf_mask) ? EEXIST : 0;
				action = UCLM_REPLACE;
				break;
			} else if ((e->pfm & pf_mask) == pf_mask && e->uc_rev == uc_rev) {
				/* new set contained in the old set, and same rev */
				action = UCLM_DISCARD;
				break;
			}
		} else { /* downgrade mode but older group and not disjoint */
			if ((e->pfm & pf_mask) == pf_mask) {
				/* new set same or contained in the old set */
				action = UCLM_DISCARD;
				break;
			}
		}

		pnext = &(e->next);
	}

	/* deleted entry cache to avoid malloc() on discard+insert */
	struct intel_uclist_entry *n = NULL;

	/*
	 * Note: we must skip the current entry if it is to be kept
	 * otherwise it is likely to get removed by the second stage
	 */
	switch (action) {
	case UCLM_REPLACE:
		if (p_ins == pnext) {
			/* replacing will preserve sorting order */
			struct intel_uclist_entry * const e = *pnext;

			e->id = id;
			e->gid = gid;
			e->flags = flags;
			/*e->cpuid = cpuid;*/
			e->pfm = pf_mask;
			e->uc_rev = uc_rev;
			e->uc = uc;
			e->uc_size = uc_size;

			pnext = &(e->next); /* skip good entry */
		} else {
			/* do it the hard way to keep things sorted */
			action = UCLM_INSERT;
			n = *pnext; /* delete and cache */
			*pnext = n->next;
		}
		break;

	case UCLM_DISCARD:
		/* Note: one downgrade mode border condition requires
		 * the second stage, we can't just exit here */
		if (!downgrade)
			return EEXIST;

		res = EEXIST;
		pnext = &((*pnext)->next); /* skip good entry */
		break;

	case UCLM_INSERT:
		break;
	}

	/* second stage: discard superseded, and insert */
	while (*pnext && (*pnext)->cpuid == cpuid) {
		struct intel_uclist_entry * const e = *pnext;

		if (((pf_mask & e->pfm) == e->pfm) &&
		    ((!downgrade && uc_rev >= e->uc_rev) ||
		     (downgrade && gid > e->gid))) {
			/* old entry is a subset of the new */

			/* remove entry from the list */
			*pnext = e->next;
			if (n == NULL) {
				n = e; /* cache to avoid malloc() */
			} else {
				free(e);
			}
		} else {
			/* locate potential insertion point... */
			if (!p_ins && pf_mask >= e->pfm)
				p_ins = pnext;

			pnext = &(e->next);
		}
	}

	if (action == UCLM_INSERT) {
		if (!n) {
			n = malloc(sizeof(struct intel_uclist_entry));
			if (unlikely(!n))
				return ENOMEM;
		}

		memset(n, 0, sizeof(struct intel_uclist_entry));
		n->id = id;
		n->gid = gid;
		n->flags = flags;
		n->cpuid = cpuid;
		n->pfm = pf_mask;
		n->uc_rev = uc_rev;
		n->uc = uc;
		n->uc_size = uc_size;

		/* prepend at insertion point */
		if (!p_ins)
			p_ins = pnext;
		n->next = *p_ins;
		*p_ins = n;

		n = NULL;
	}

	free(n);

	return res;
}

/* Microcode bundle handling */

/**
 * add_intel_microcode_bundle() - add a microcode bundle to set
 * @mcb_filename:	filename (metadata)
 * @mcb_id:		bundle id (metadata)
 * @mcb:		pointer to the microcode bundle data
 * @mcb_size:		size of the microcode bundle data
 *
 * Note: the memory pointed to by @mcb will be freed when
 * the bundles are freed by free_intel_microcode_bundles().
 *
 * Returns 0 if the bundled was added, or ENOMEM
 */
static int add_intel_microcode_bundle(const char * const mcb_filename,
				      unsigned int mcb_id,
				      const void * const mcb,
				      const size_t mcb_size)
{
	assert(mcb && mcb_size >= INTEL_UC_MINSIZE);
	assert(mcb_filename);
	assert(microcode_bundles_tail && !(*microcode_bundles_tail));

	struct microcode_bundle *b = malloc(sizeof(struct microcode_bundle));
	if (!b)
		return ENOMEM;

	memset(b, 0, sizeof(struct microcode_bundle));
	b->id = mcb_id;
	b->filename = strdup(mcb_filename);
	b->size = mcb_size;
	b->data = mcb;

	/* add to end of list */
	*microcode_bundles_tail = b;
	microcode_bundles_tail = &(b->next);

	return 0;
}

static void free_intel_microcode_bundles(void) __attribute__((unused));
static void free_intel_microcode_bundles(void)
{
	struct microcode_bundle *p, *t;

	p = microcode_bundles;
	while (p) {
		t = p;
		p = t->next;
		free((void *)(t->filename));
		free((void *)(t->data));
		free(t);
	}

	microcode_bundles = NULL;
	microcode_bundles_tail = &microcode_bundles;
}

static inline uint32_t xx_asc2digit(const char c)
{
	/* c must be [0-9a-fA-F] */
	return 9*((uint32_t)c >> 6) + ((uint32_t)c & 0xf);
}

static void xx_parser_exception(void)
{
	fast_parser_exceptions++;
	if (fast_parser_exceptions > IUCODE_DAT_FAST_EXMAX)
		fast_parser_enabled = 0;
}

/* For whatever idiotic reason, a static inline doesn't work as well */
#define IS_INVALID_HEX(x) ((x) < '0' || (x) > 'f' || ((x) > '9' && (x) < 'A') || ((x) > 'F' && (x) < 'a'))

/* caller must ensure sanity of all parameters */
static inline int parse_u32_fast(char *s, char **e, uint32_t * const res)
{
	const char * ss = s;

	/* fast path: 0x1234aBcD */
	if (likely((s[0] == '0' && s[1] == 'x') && fast_parser_enabled)) {
		uint32_t r = 0;
		int i;
		s += 2;
		for (i = 0; i < 8; i++) {
			const char c = *s;

			if (unlikely(IS_INVALID_HEX(c))) {
				xx_parser_exception();
				return parse_u32(ss, e, 0, res);
			}

			r = (r << 4) + xx_asc2digit(c);
			s++;
		}

		/* if the next char is not a digit, the fast path was successful */
		if (likely(IS_INVALID_HEX(*s))) {
			*res = r;
			*e = s;
			return 0;
		}

		xx_parser_exception();
	}

	return parse_u32(ss, e, 0, res);
}

#undef IS_INVALID_HEX

#ifndef HAVE_FGETS_UNLOCKED
#define fgets_unlocked fgets
#endif

#ifdef static_assert
	static_assert(IUCODE_MAX_MCU_FILE_SIZE <= SIZE_MAX,
		"IUCODE_MAX_MCU_FILE_SIZE must fit in size_t");
#endif

static int load_intel_microcode_dat(FILE *fp,
				    void ** const mcb, size_t * const mcb_length,
				    unsigned long int * const lineno)
{
	const size_t buffer_size_granularity = IUCODE_TYPICAL_MCU_FILE_SIZE / sizeof(uint32_t);
	const size_t buffer_limit = IUCODE_MAX_MCU_FILE_SIZE / sizeof(uint32_t);
	char *line_buffer = NULL;
	size_t mcb_buflen;
	uint32_t *mcb_buffer = NULL;
	uint32_t *rp;
	size_t length;
	size_t pos = 0;
	unsigned long int lines = 0;
	int nulateol;
	int err;

	assert(mcb);
	assert(mcb_length);

	if (ferror(fp))
		return -EIO;

	/* reset parser fast-path */
	fast_parser_enabled = 1;
	fast_parser_exceptions = 0;

	line_buffer = malloc(IUCODE_DAT_LBUF_SIZE);
	mcb_buflen = buffer_size_granularity;
	mcb_buffer = malloc(mcb_buflen * sizeof(uint32_t));
	if (!mcb_buffer || !line_buffer) {
		err = ENOMEM;
		goto err_out_nolock;
	}

#ifdef HAVE_FLOCKFILE
	flockfile(fp);
#endif

	err = EINVAL;
	nulateol = 0;
	while (likely(fgets_unlocked(line_buffer, IUCODE_DAT_LBUF_SIZE, fp) != NULL)) {
		/*
		 * Data lines are of the form "0x%x, 0x%x, 0x%x, 0x%x,"
		 * Comment lines start with a single "/"
		 *
		 * As long as the initial comment block is stripped, the file
		 * would be valid C source to initialize an array of 32-bit
		 * integers.
		 *
		 * This parser will accept empty lines, and ignore initial
		 * white space.  NULs cannot be considered white space due to
		 * fgets() not being NUL-safe, so they're invalid.
		 *
		 * It will accept a comment after valid data, as long as
		 * there's white space or a comma before the comment start.
		 *
		 * It does not properly handle multi-line C comments
		 * unless they start with / in every line.
		 *
		 * It will accept a file that does not end with an EOL mark.
		 *
		 * This parser is tuned to the exact format Intel has been
		 * using for .dat files since 2008 (which are the ones that
		 * can be distributed by Linux distros).
		 */
		char *lp = line_buffer;

		/* last line "ended" by a NUL, but it was not EOF */
		if (unlikely(nulateol))
			goto err_out;

		lines++;

		if (unlikely(!*lp)) /* "line" of one or more NULs */
			goto err_out;

		while (isspace(*lp))
			lp++;

		while (likely(*lp && *lp != '/')) {
			char *ep;

			if (unlikely(parse_u32_fast(lp, &ep, &(mcb_buffer[pos]))))
				goto err_out;

			pos++;
			if (unlikely(mcb_buflen <= pos)) {
				/* expand buffer */
				if (unlikely(buffer_limit - mcb_buflen < buffer_size_granularity)) {
					err = EFBIG;
					goto err_out;
				}
				mcb_buflen += buffer_size_granularity;
				rp = realloc(mcb_buffer,
					     mcb_buflen * sizeof(uint32_t));
				if (unlikely(!rp)) {
					err = ENOMEM;
					goto err_out;
				}
				mcb_buffer = rp;
			}

			/* seek to start of next value or to EOL */
			lp = ep;
			while (isspace(*lp))
				lp++;
			if (likely(*lp == ',')) {
				/* skip ",[[:space:]]*" */
				do { lp++; } while (isspace(*lp));
				/* we're either at EOL or next value, now */
			} else if (unlikely(*lp && (lp == ep || *lp != '/'))) {
				goto err_out;
			}
		}

		/* if the line ends without EOL, next must be EOF */
		nulateol = (!*lp && *(lp-1) != '\n');
	}
	if (ferror(fp)) {
		err = -errno; /* negative! */
		goto err_out;
	}
	if (!pos && feof(fp)) {
		/* empty data file */
		err = ENOENT;
		goto err_out;
	}

	length = pos * sizeof(uint32_t);
	if (length < INTEL_UC_MINSIZE) {
		err = EINVAL;
		goto err_out;
	}

	/* truncate buffer if too large */
	rp = realloc(mcb_buffer, length);
	if (!rp) {
		err = ENOMEM;
		goto err_out;
	}

	*mcb = rp;
	*mcb_length = length;

	err = 0;

err_out:
#ifdef HAVE_FLOCKFILE
	funlockfile(fp);
#endif

err_out_nolock:
	if (err) {
		free(mcb_buffer);
		*mcb = NULL;
		*mcb_length = 0;
	}

	if (lineno)
		*lineno = lines;

	free(line_buffer);
	return err;
}

static int scan_and_pack_microcodes(uint8_t * const mcb, size_t * const mcb_size)
{
	uint8_t *mcb_e, *bs, *be;
	size_t  mcb_len, b_len;
	unsigned long int bc, ucc;
	int r;

	mcb_len = *mcb_size;
	mcb_e = mcb;
	bs = mcb;
	ucc = 0;
	bc = 0;

	do {
		r = intel_ucode_scan_for_microcode((const void **)&bs,
						   (const void **)&be,
						   &b_len, &mcb_len);
		if (r > 0) {
			/* (in)sanity checks */
			if (unlikely(bs < mcb_e || be < bs || b_len > *mcb_size || mcb_len > *mcb_size)) {
				print_err("Internal error: got insane results from intel_ucode_scan_for_microcode()");
				exit(EXIT_SWFAILURE); /* alternative: return EFAULT */
			}

			print_msg(3, "microcode scan: microcode block at position %td with %d microcode(s), size %zu bytes",
				  bs - mcb, r, b_len);

			if (mcb_e != bs)
				memmove(mcb_e, bs, b_len);
			mcb_e += b_len;
			bs = be;
			bc++;
			ucc += (unsigned int)r;  /* r > 0 enforced in this branch */
		} else if (unlikely(r < 0)) {
			print_err("Internal error: intel_ucode_scan_for_microcode() went out-of-bounds");
			exit(EXIT_SWFAILURE); /* alternatively: return EFAULT */
		}
	} while (r > 0);

	mcb_len = (size_t)(mcb_e - mcb); /* mcb <= mcb_e, delta constrained by mcb_size */
	*mcb_size = mcb_len;

	print_msg(3, "microcode scan: found %lu microcode block(s) with %lu microcode(s), total size: %zu bytes",
		  bc, ucc, mcb_len);

	return (mcb_len) ? 0 : ENOENT;
}

static int load_intel_microcode_bin(int fd,
				    void ** const mcb, size_t * const mcb_length,
				    const int scan)
{
	const size_t buffer_size_granularity = IUCODE_TYPICAL_MCU_FILE_SIZE;
	const size_t buffer_limit = IUCODE_MAX_MCU_FILE_SIZE;

	int file_size_known;
	struct stat stat;

	size_t mcb_size;
	size_t mcb_space_left;
	uint8_t *mcb_buffer = NULL;
	uint8_t *rp;

	size_t pos;
	int err;

	assert(mcb);
	assert(mcb_length);

	/* Try to get file size beforehand */
	if (fstat(fd, &stat))
		return -errno; /* negative! */
	if (S_ISREG(stat.st_mode)) {
		if (stat.st_size < 0 || (uintmax_t)stat.st_size > buffer_limit)
			return EFBIG;
		mcb_size = (size_t)stat.st_size;
		file_size_known = 1;
	} else {
		mcb_size = buffer_size_granularity;
		file_size_known = 0;
	}

	/* sanity check size */
	if (mcb_size < INTEL_UC_MINSIZE) {
		err = (mcb_size && !scan) ? EINVAL : ENOENT;
		goto err_out;
	}

	mcb_buffer = malloc(mcb_size);
	if (!mcb_buffer) {
		err = ENOMEM;
		goto err_out;
	}

	err = 0;
	pos = 0;
	mcb_space_left = mcb_size;
	do {
		ssize_t rc;

		rc = read(fd, mcb_buffer + pos,
			  (mcb_space_left < SSIZE_MAX) ? mcb_space_left : SSIZE_MAX);
		if (rc == -1) {
			if (errno == EINTR)
				continue;
			err = -errno; /* negative! */
			goto err_out;
		}

		/* POSIX ensures 0 <= rc <= SSIZE_MAX here */
		pos += (size_t)rc;
		mcb_space_left -= (size_t)rc;

		if (rc == 0)
			break; /* EOF */
		if (file_size_known && !mcb_space_left)
			break; /* Read entire file */

		if (!mcb_space_left) {
			if (unlikely(buffer_limit - mcb_size < buffer_size_granularity)) {
				err = EFBIG;
				goto err_out;
			}
			mcb_size += buffer_size_granularity;
			mcb_space_left += buffer_size_granularity;
			rp = realloc(mcb_buffer, mcb_size);
			if (!rp) {
				err = ENOMEM;
				goto err_out;
			}
			mcb_buffer = rp;
		}
	} while (1);

	mcb_size = pos;
	if (mcb_size < INTEL_UC_MINSIZE) {
		err = (mcb_size && !scan) ? EINVAL : ENOENT;
		goto err_out;
	}

	if (scan) {
		err = scan_and_pack_microcodes(mcb_buffer, &mcb_size);
		if (err)
			goto err_out;
	}

	rp = realloc(mcb_buffer, mcb_size);
	if (!rp && mcb_size) {
		err = ENOMEM;
		goto err_out;
	}

	/* rp is allowed to be NULL when mcb_size == 0 */

	*mcb = rp;
	*mcb_length = mcb_size;

	return 0;

err_out:
	free(mcb_buffer);
	*mcb = NULL;
	*mcb_length = 0;

	return err;
}

static int load_intel_microcode_file(int fd, FILE *fp,
				     const char * const fn,
				     const intel_ucode_file_type_t ftype)
{
	int err = 0;

	void *mcb = NULL;
	size_t mcb_length = 0;

	unsigned long int tl = 0;

	switch (ftype) {
	case INTEL_UC_FT_SCAN:
	case INTEL_UC_FT_BIN:
		{
			const int scanmode = !!(ftype == INTEL_UC_FT_SCAN);

			print_msg(3, "%s: loading (%sbinary mode)", fn,
				     (scanmode) ? "microcode recovery " : "");
			err = load_intel_microcode_bin(fd, &mcb, &mcb_length, scanmode);
		}
		break;

	case INTEL_UC_FT_DAT:
		if (!fp)
			fp = fdopen(fd, "r");

		if (fp) {
			print_msg(3, "%s: loading (.dat mode)", fn);
			err = load_intel_microcode_dat(fp, &mcb, &mcb_length, &tl);
			if (!fast_parser_enabled)
				print_msg(3, "%s: had to switch over to slow parser mode", fn);
		} else {
			err = -errno;
		}
		break;

	default:
		err = ENOTSUP;
	}

	if (err < 0) { /* error from read() or stdio */
		print_err("%s: could not read: %s", fn, strerror(-err));
		goto err_out;
	}

	/*
	 * If a loader returns a zero-length microcode bundle (mcb_length
	 * was set to 0 by the loader, or mcb is NULL), or an error, the
	 * bundle will NOT be stored for further processing.
	 *
	 * If err == 0, the bundle will be assigned an ID, and listed to
	 * the user when in verbose mode.
	 *
	 * If err == ENOENT, the user is notified that no microcodes were
	 * found in that data file and that the file is being skipped.
	 * It will not be reported as an error to the caller.
	 *
	 * Otherwise, the user will be notified of an error, and the error
	 * will be reported to the caller, which may abort execution of
	 * iucode_tool.
	 */

	if (!err && mcb && mcb_length) {
		err = add_intel_microcode_bundle(fn, next_bundle_id,
						 mcb, mcb_length);
		if (!err)
			mcb = NULL; /* someone else will free it */
	}

	switch (err) {
	case 0:
		print_msg(3, "loaded microcode bundle %u: %s (%zu bytes)",
			  next_bundle_id, fn, mcb_length);
		next_bundle_id++;
		break;
	case ENOENT:
		print_err("%s: no microcodes found in data file, skipping...", fn);
		err = 0;
		break;
	case ENOMEM:
		print_err("%s: could not allocate memory while loading", fn);
		break;
	case EINVAL:
		if (!tl) {
			print_err("%s: invalid file format", fn);
		} else {
			print_err("%s: line %lu: invalid file format", fn, tl);
		}
		break;
	case EFBIG:
		print_err("%s: cowardly refusing to load an insanely large data file", fn);
		break;
	}

err_out:
	if (fp)
		fclose(fp); /* also closes fd */
	else if (fd != -1)
		close(fd);

	free(mcb);

	return err;
}

static int load_intel_microcode(const char * path,
				const intel_ucode_file_type_t baseftype)
{
	int fd = -1;

	DIR *dir;
	int err;

	char fnbuf[PATH_MAX];
	const char *fn;
	intel_ucode_file_type_t ftype;

	assert(path);

	/* Should we read from stdin ? */
	if (is_dash(path)) {
		/* read stdin only once, ignore further requests */
		if (processed_stdin)
			return 0;

		processed_stdin = 1;
		ftype = baseftype;

		/* default to .dat mode for stdin */
		if (ftype == INTEL_UC_FT_UNKNOWN)
			ftype = INTEL_UC_FT_DAT;

		/* paranoia */
		fd = fileno(stdin);
		if (unlikely(fd == -1)) {
			/* shouldn't happen unless iucode_tool starts doing
			 * freopen(stdin).  Refer to sanitize_std_fds(). */
			err = errno;
			print_err("stdin: internal error: %s", strerror(err));
			return -err;
		}

		return load_intel_microcode_file(fd, stdin,
						"(stdin)", ftype);
	}

	dir = NULL;
	fn = path;

	do {
		struct stat st;
		struct dirent *dentry;

		err = 0;

		if (fd != -1) {
			close(fd);
			fd = -1;
		}

		if (dir) {
			errno = 0;
			dentry = readdir(dir);
			if (!dentry) {
				err = errno;
				if (unlikely(err)) {
					print_err("%s: cannot walk directory: %s",
						  path, strerror(err));
					err = -err;
				}
				break; /* finish/abort walk */
			}

			if (dentry->d_name[0] == '.')
				continue;

			int s = snprintf(fnbuf, sizeof(fnbuf), "%s/%s",
					 path, dentry->d_name);
			if (unlikely(s < 1 || (unsigned int)s >= sizeof(fnbuf))) {
				print_err("%s/%s: path too long",
					  path, dentry->d_name);
				err = -ENAMETOOLONG;
				continue;
			}
			fn = fnbuf;
			fd = openat(dirfd(dir), dentry->d_name, O_RDONLY);
		} else {
			fd = open(fn, O_RDONLY);
		}
		if (unlikely(fd == -1)) {
			err = errno;
			print_err("%s: cannot open: %s", fn, strerror(err));
			err = -err;
			continue;
		}
		if (unlikely(fstat(fd, &st) == -1)) {
			err = errno;
			print_err("%s: cannot stat inode: %s", fn,
				  strerror(err));
			err = -err;
			continue;
		}
		if (S_ISDIR(st.st_mode)) {
			if (!dir) {
				dir = fdopendir(fd);
				if (!dir) {
					err = errno;
					print_err("%s: cannot open directory: %s",
						  path, strerror(err));
					err = -err;
					continue;
				}
				print_msg(3, "%s: reading directory", fn);
				/*
				 * fd is now out-of-scope due to successful
				 * fdopendir(fd), and must NOT be closed.
				 */
				fd = -1;
			} else {
				print_msg(1, "%s: skipping nested directory: %s",
					  path, fn);
			}
			continue;
		}

		if (S_ISREG(st.st_mode) && st.st_size == 0) {
			print_msg(3, "skipping empty file: %s", fn);
			continue;
		}

		ftype = baseftype;
		if (ftype == INTEL_UC_FT_UNKNOWN && S_ISREG(st.st_mode)) {
			char *p;

			/* try to guess based on extension */
			p = strrchr(fn, '.');
			if (p) {
				if (!strcasecmp(p + 1, "dat"))
					ftype = INTEL_UC_FT_DAT;
				/*
				 * Unneeded due to default to bin mode
				 *
				else if (!strcasecmp(p + 1, "bin"))
					ftype = INTEL_UC_FT_BIN;
				else if (!strcasecmp(p + 1, "fw"))
					ftype = INTEL_UC_FT_BIN;
				*/
			}
		}

		/* default to bin mode */
		if (ftype == INTEL_UC_FT_UNKNOWN)
			ftype = INTEL_UC_FT_BIN;

		err = load_intel_microcode_file(fd, NULL, fn, ftype);
		fd = -1; /* fd closed by load_intel_microcode_file() */
	} while (dir && (!err || ignore_bad_ucode));

	if (dir)
		closedir(dir);
	if (fd != -1)
		close(fd);
	return err;
}

/* Microcode write (binary format) and kernel upload */

/*
 * The compiler would optimize this for us properly, but
 * it will warn about it, and so will the static checker.
 */
static inline size_t ssizemax_clamp(const uint32_t s)
{
#if UINT32_MAX > SSIZE_MAX
	return (s < SSIZE_MAX) ? (size_t)s : SSIZE_MAX;
#else
	return s;
#endif
}

/* write_data returns -1 on error, with errno set */
static int write_data(int fd,
		      const void * const data,
		      const uint32_t size)
{
	const char *p = data;
	uint32_t len = size;
	ssize_t s;

	while (len > 0) {
		s = write(fd, p, ssizemax_clamp(len));
		if (s >= 0) {
			/* POSIX ensures 0 <= s <= min(SSIZE_MAX,len) */
			p += (size_t)s;
			len -= (size_t)s;
		} else if (errno != EINTR) {
			return -1;
		}
	}

	return 0;
}

static void log_microcode_action(const char * const action,
				const char * const devname,
				const struct intel_uclist_entry * const uce)
{
	print_msg_u("%s: %s microcode " UCODE_ID_FMT_UU
		    " (sig 0x%08x, pf_mask 0x%02x, rev 0x%04x)",
		    devname, action, uce->gid, uce->id,
		    uce->cpuid, uce->pfm, (uint32_t) uce->uc_rev);
}

/**
 * upload_intel_microcodes() - uploads microcodes to kernel device
 * @devname:		device path
 * @uc_write_list:	uclist with the microcodes to write
 *
 * returns 0 if successful, or errno if a problem happens
 */
static int upload_intel_microcodes(const char * const devname,
				  struct intel_uclist_entry *uc_write_list)
{
	int fd;
	struct stat stat;
	int err = 0;
	unsigned long long int total_written = 0;
	unsigned int entries_written = 0;

	assert(uc_write_list);
	assert(devname);

	fd = open(devname, O_WRONLY);
	if (fd == -1) {
		err = errno;
		print_err("%s: cannot open for writing: %s",
			  devname, strerror(err));
		return err;
	}
	/* Is it a char device? */
	if (fstat(fd, &stat) == -1) {
		err = errno;
		print_err("%s: cannot stat: %s", devname, strerror(err));
		close(fd);
		return err;
	}
	if (!S_ISCHR(stat.st_mode)) {
		print_err("%s: not a character device", devname);
		close(fd);
		return EINVAL;
	}

	while (uc_write_list && uc_write_list->uc) {
		if (likely(!(uc_write_list->flags & INTEL_UCLE_NOWR))) {
			if (verbosity >= 3)
				log_microcode_action("uploading",
						     devname, uc_write_list);

			if (write_data(fd, uc_write_list->uc,
				       uc_write_list->uc_size)) {
				err = errno;
				print_err("%s: write error: %s",
					  devname, strerror(err));
				break;
			}
			total_written += uc_write_list->uc_size;
			entries_written++;
		} else {
			if (verbosity >= 3)
				log_microcode_action("skipping",
						     devname, uc_write_list);
		}
		uc_write_list = uc_write_list->next;
	}

	if (close(fd)) {
		err = errno;
		print_err("%s: error while closing device: %s",
			  devname, strerror(err));
	}

	if (!err)
		print_msg(2, "%s: %u microcode entries uploaded, %llu bytes",
			  devname, entries_written, total_written);

	return err;
}

static time_t bcd_date_to_time(const uint32_t d)
{
	unsigned int day, month, year;
	struct tm tm;

	/* packed BCD already validated */
	day   = ( d        & 0xf) +
		((d >>  4) & 0xf) * 10;

	month = ((d >>  8) & 0xf) +
	        ((d >> 12) & 0xf) * 10;

	year  = ((d >> 16) & 0xf) +
	        ((d >> 20) & 0xf) * 10 +
	        ((d >> 24) & 0xf) * 100 +
	        ((d >> 28) & 0xf) * 1000;

	/* clamp to something filesystem-friendly */
	memset(&tm, 0, sizeof(tm));
	tm.tm_hour = 12;
	tm.tm_mday = (int)(day);
	tm.tm_mon  = (likely(month > 0))? (int)month - 1 : 0;
	tm.tm_year = (likely(year >= 1970))? (int)year - 1900 : 1970;

	return timegm(&tm);
}

#define CPIO_STATIC_HDRSIZE (6 + 13*8)
#define CPIO_MAX_HDRSIZE (CPIO_STATIC_HDRSIZE + sizeof(LINUX_CPIO_UCODE_NAME))

/* if size is zero, assume it is a directory */
static int xx_write_cpio_hdrentry(int fd, const char * const name,
				 const size_t size, const uint32_t t,
				 size_t * const pos)
{
	static unsigned int ino = 100;

	char buf[CPIO_MAX_HDRSIZE + 16]; /* worst case padding */
	size_t nsize, bufsize, s;

	assert(name);
	nsize = strlen(name) + 1;

	ino++;

	/* pad to DWORD, assume already DWORD-aligned */
	bufsize = CPIO_STATIC_HDRSIZE + nsize;
	bufsize += (4 - bufsize % 4) % 4;

	/* Gross hack to work around a Linux kernel bug: for file
	 * entries, force file data into a 16-byte alignment by
	 * appending NULs to the file name.  Verified to be compatible
	 * with GNU pax, and GNU cpio.
	 *
	 * See also xx_write_cpio_pad_member() */
	s = (size) ? (16 - (*pos + bufsize) % 16) % 16 : 0;
	bufsize += s;
	nsize += s;

	*pos += bufsize;

	memset(buf, 0, sizeof(buf));
	snprintf(buf, sizeof(buf),
	  "070701" /* signature, new ASCII portable format, no CRC */
	  "%08X%08X%08X%08X%08X%08X%08zX%08X%08X%08X%08X%08zX%08X%s",
	  ino,                                                     /* inode */
	  size ? LINUX_CPIO_FILE_MODE : LINUX_CPIO_DIR_MODE,        /* mode */
	  0U, 0U,                                               /* uid, gid */
	  size ? 1U : 2U, t, size,                    /* nlink, mtime, size */
	  3U, 1U, 0U, 0U, nsize, 0U,              /* devj, devm, nsize, CRC */
	  name);                                               /* name, pad */

	return write_data(fd, buf, bufsize);
}

/*
 * Implement an alternative to the hack in xx_write_cpio_hdrentry() to
 * work around a Linux kernel bug: right before adding the cpio header for
 * the microcode file member, insert an header entry that is sized so that
 * it will force the data section for the next member into a 16-byte
 * alignment.
 *
 * xx_write_cpio_pad_member() is optional.  If it isn't used, or used
 * incorrectly, xx_write_cpio_hdrentry() will extend the microcode data
 * file name with NULs to compensate.
 *
 * We could have it all precalculated and hardcoded as constants instead,
 * but that's annoying.
 */
static int xx_write_cpio_pad_member(int fd, const size_t nl,
				    const uint32_t t, size_t * const pos)
{
	char pad_fn[] = LINUX_CPIO_UCODE_PADNAME;
	size_t s;

	/* Calculate where the microcode data would end up without
	 * the addition of a padding member */
	s = *pos + CPIO_STATIC_HDRSIZE + nl + 1;
	s += (4 - s % 4) % 4; /* pad filename to dword boundary */

	/* Detect when no padding member would be necessary.
	 *
	 * This will never trigger unless the kernel's ABI changes.
	 * But having it here makes me happier, so... */
	if (unlikely(s % 16 == 0))
		return 0;

	/* Calculate the amount of (negative) padding required.  By
	 * varying the size of the padding member's name, we can
	 * offset the next member's (microcode file) *data* section
	 * into a 16-byte alignment */
	s += CPIO_STATIC_HDRSIZE + strlen(LINUX_CPIO_UCODE_PADNAME) + 1;
	pad_fn[strlen(LINUX_CPIO_UCODE_PADNAME) - s % 16] = 0;

	return xx_write_cpio_hdrentry(fd, pad_fn, 0, t, pos);
}

/**
 * write_cpio_header() - writes cpio header for early microcode
 * @fd:			file to write to
 * @size:		size of the file being written, for the header
 * @date:		date for members of the archive, in BCD
 * 			YYYYMMDD format.
 * @parents:		output headers for parent dirs when nonzero
 * @err:		set to errno if an error happens
 *
 * returns the number of bytes written if successful, or -1
 * if a problem happens
 *
 * Write cpio header.  For Intel microcodes, the kernel ABI
 * defines that they should be in a single file, named
 * kernel/x86/microcode/GenuineIntel.bin.  We use the 070701
 * format ("newc" format for GNU cpio).  To work around a kernel
 * bug, we ensure the file data will be 16-byte aligned.
 *
 * The kernel loader cares not for leading parent dirs, but
 * when they are missing, the file won't be available to the
 * regular initramfs.
 *
 * The maximum file size supported by the "newc" format is 4GiB.
 */
static ssize_t write_cpio_header(int fd, const size_t size,
				 const uint32_t date, const int parents,
				 int * const err)
{
	char fn[] = LINUX_CPIO_UCODE_NAME;
	size_t pos = 0;
	time_t uc_time = bcd_date_to_time(date);
	uint32_t t;

	assert(err);

	if (unlikely(size > UINT32_MAX))
	    return -EFBIG;

	/* cpio newc format is not y2038-safe */
	t = (uc_time > INT32_MAX) ? INT32_MAX : (uint32_t) uc_time;

	/*
	 * the early initramfs kernel loader doesn't need the directories
	 * to be created before the file can be accessed, but the regular
	 * initramfs kernel loader does.  Including them makes the file
	 * available inside the regular initramfs.
	 */
	if (parents) {
		char *p1, *p2;

		p1 = fn;
		do {
			p2 = strchr(p1, '/');
			if (p2) {
				*p2 = 0;
				if (xx_write_cpio_hdrentry(fd, fn, 0, t, &pos))
					goto err_exit;
				*p2 = '/';
				p1 = ++p2;
			}
		} while (p2);

		if (xx_write_cpio_pad_member(fd,
					     strlen(LINUX_CPIO_UCODE_NAME),
					     t, &pos))
			goto err_exit;
	}

	if (xx_write_cpio_hdrentry(fd, fn, size, t, &pos))
		goto err_exit;

	assert(pos < SSIZE_MAX);
	return (ssize_t)pos;

err_exit:
	*err = errno;
	return -1;
}

/**
 * write_cpio_trailer() - write a cpio EOF trailer and pad
 * @fd:			file to write to
 * @pos:		file position
 * @blksize:		cpio block size (for padding)
 * @err:		set to errno if an error happens
 *
 * returns the number of bytes written if successful, or -1
 * if a problem happens
 *
 * Write a cpio EOF trailer, padding with NULs to the nearest
 * @blksize boundary.  @pos is used to calculate the amount of
 * padding, instead of requiring a lseek().
 *
 * @blksize should be either 16 (non-standard), 512 or 1024.
 */
static ssize_t write_cpio_trailer(int fd, const size_t pos,
				  const size_t blksize, int * const err)
{
	const char cpio_trailer[] = "070701"
		/* inode, mode,  uid,   gid */
		"00000000" "00000000" "00000000" "00000000" 
		/* nlink, mtime, size,  devj */
		"00000001" "00000000" "00000000" "00000000"
		/* devm,  rdevj, rdevm, name size */
		"00000000" "00000000" "00000000" "0000000B"
		/* checksum, name */
		"00000000" "TRAILER!!!";
	const size_t s = sizeof(cpio_trailer) +
		((blksize - ((pos + sizeof(cpio_trailer)) % blksize)) % blksize);
	char *buf;

	assert(err);
	assert(blksize <= 1024U && (blksize % 16) == 0);

	buf = malloc(s);
	if (!buf) {
		*err = ENOMEM;
		return -1;
	}

	/* we depend on the buffer-fill strncpy() semantics here */
	int rc = write_data(fd, strncpy(buf, cpio_trailer, s), s);
	if (rc)
		*err = errno;

	free(buf);

	/* ssize_t cast valid for any sane blksize */
	return (rc) ? rc : (ssize_t)s;
}


/**
 * write_intel_microcodes() - writes microcodes to bin file
 * @dirfd:		directory where to create file
 * @filename:		file to write to
 * @ft:			file type
 * @uc_write_list:	uclist with the microcodes to write
 *
 * returns 0 if successful, errno if a problem happens,
 * or ENODATA if all microcodes in @uc_write_list have the
 * INTEL_UCLE_NOWR flag set (in which case, nothing is done
 * to the file).
 *
 * @dirfd should be either an open directory for openat(), or
 * the special value AT_FDCWD.
 *
 * @ft should be zero for binary format, 1 for Linux early
 * initramfs cpio format.
 *
 * binary format is the Intel-specified format for binary microcode,
 * used by the Linux firmware loader and a few other operating
 * systems.
 *
 * Linux early initramfs cpio format will write microcodes inside
 * a cpio 070701 (New portable ASCII format without CRC) wrapper,
 * suitable for early microcode loading for Linux kernels v3.9 and
 * newer.  This file should be prepended to the compressed initramfs
 * image.  It *must* be 16-byte aligned to the start of the
 * early initramfs image.
 */
static int write_intel_microcodes(int dirfd,
				  const char * const filename, const int ft,
				  struct intel_uclist_entry *uc_write_list)
{
	int fd;
	int err = 0;
	unsigned long long int total_written;
	unsigned int entries_written = 0;
	struct intel_uclist_entry *e;
	uint32_t latest_date;

	assert(uc_write_list);
	assert(filename);

	/*
	 * precalculate raw file size. It is less annoying than
	 * having to seek back to write it later in cpio mode
	 *
	 * while at it, record the latest microcode date found
	 * in the set of microcodes that will be written
	 */
	e = uc_write_list;
	total_written = 0;
	latest_date = 0;
	while (e && e->uc) {
		if (likely(!(e->flags & INTEL_UCLE_NOWR))) {
			const uint32_t d = intel_ucode_getdate_bcd(e->uc);
			if (d > latest_date)
				latest_date = d;

			total_written += e->uc_size;
		}
		e = e->next;
	}
	if (unlikely(!total_written)) {
		return ENODATA;	/* don't create empty files */
	} else if (unlikely(ft == 1 && total_written > UINT32_MAX)) {
		/* file too large for cpio archive members */
		print_err("%s: too much data", filename);
		return EFBIG;
	}

	/* Unlink first */
	if (unlink_files) {
		if (unlinkat(dirfd, filename, 0) == -1) {
			if (errno != ENOENT) {
				err = errno;
				print_err("%s: cannot unlink: %s", filename,
					  strerror(err));
				return err;
			}
		} else {
			print_msg(3, "unlinked %s", filename);
		}
	}

	fd = openat(dirfd, filename, O_CREAT | O_WRONLY | O_EXCL,
		    S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
	if (fd == -1) {
		err = errno;
		print_err("%s: cannot write to, or create file: %s",
			  filename, strerror(err));
		return err;
	}

	if (ft == 1) {
		ssize_t rc = write_cpio_header(fd, total_written,
					latest_date, cpio_parentdirs,
					&err);
		if (rc < 0)
			goto error;
		total_written += (size_t)rc;
	}

	while (uc_write_list && uc_write_list->uc) {
		if (likely(!(uc_write_list->flags & INTEL_UCLE_NOWR))) {
			if (verbosity >= 3)
				log_microcode_action("writing",
						     filename, uc_write_list);

			if (unlikely(write_data(fd, uc_write_list->uc,
					        uc_write_list->uc_size))) {
				err = -errno;
				goto error;
			}

			entries_written++;
		} else {
			if (verbosity >= 3)
				log_microcode_action("skipping",
						     filename, uc_write_list);
		}
		uc_write_list = uc_write_list->next;
	}

	if (ft == 1) {
		ssize_t rc = write_cpio_trailer(fd, total_written,
						cpio_blocksize, &err);
		if (unlikely(rc < 0))
			goto error;
		total_written += (size_t)rc;
	}

	if (fdatasync(fd)) {
		err = errno;
		print_err("%s: error while flushing file data: %s",
			  filename, strerror(err));
	}
	if (close(fd)) {
		err = errno;
		print_err("%s: error while closing file: %s",
			  filename, strerror(err));
	}

	if (!err)
		print_msg(2, "%s: %u microcode entries written, %llu bytes",
			  filename, entries_written, total_written);

	return err;

error:
	print_err("%s: write error: %s", filename, strerror(err));
	return err;
}

/* Microcode filtering */

#define PFMASK_MATCH_ANY 0xffffffffU

static int is_in_date_range(const struct intel_ucode_metadata * const m)
{
	const uint32_t d = ((unsigned int)m->date_year << 16) |
	                   ((unsigned int)m->date_month << 8) |
			   ((unsigned int)m->date_day);
	return !!((d > datefilter_min) && (d < datefilter_max));
}

static void free_filter_list(struct microcode_filter_entry *f)
{
	static struct microcode_filter_entry *p;

	while (f) {
		p = f;
		f = f->next;
		free(p);
	}
}

static int xx_merge_filter(struct microcode_filter_entry * const f,
			 const uint32_t pf_mask, const int32_t rev,
			 const enum iuc_rev_match_mode rev_match,
			 const int invert)
{
	/* FIXME: we could do better, sometimes it is possible to merge */
	if (f->rev_match != rev_match || f->rev != rev)
		return 0; /* can't merge */

	if (f->invert == invert) {
		f->pfm |= pf_mask;
	} else if (!(f->invert)) {
		f->pfm &= ~pf_mask;
	} else {
		f->invert = 0;
		f->pfm = pf_mask;
	}

	return 1; /* merged */
}

static int add_filter_to_list(uint32_t cpuid, uint32_t pf_mask, int32_t rev,
			      enum iuc_rev_match_mode rev_match, int invert,
			      struct microcode_filter_entry ** const base)
{
	struct microcode_filter_entry **pnext, *n;

	assert(base);

	if (!cpuid || cpuid == IUCODE_FILTERMASK_SCANCPUS)
		return EINVAL;

	if (!pf_mask)
		pf_mask = PFMASK_MATCH_ANY;

	pnext = base;
	/* search for matching cpuid */
	while (*pnext && (*pnext)->cpuid < cpuid)
		pnext = &((*pnext)->next);

	if (*pnext && (*pnext)->cpuid == cpuid) {
		if (((pf_mask & (*pnext)->pfm) == pf_mask) &&
		    (*pnext)->rev_match == rev_match &&
		    (*pnext)->rev == rev &&
		    (*pnext)->invert == invert) {
			/* found equivalent, report it */
			return EEXIST;
		}

		/* merge when possible */
		if (xx_merge_filter(*pnext, pf_mask, rev, rev_match, invert))
			return 0;
	}

	/* insert before */
	n = malloc(sizeof(struct microcode_filter_entry));
	if (!n)
		return ENOMEM;
	n->cpuid = cpuid;
	n->pfm = pf_mask;
	n->rev = rev;
	n->rev_match = rev_match;
	n->invert = invert;
	n->next = *pnext;
	*pnext = n;

	return 0;
}

/* After calling this, "entries" becomes invalid */
static void add_filter_list_to_list(struct microcode_filter_entry ** const base,
				    struct microcode_filter_entry *entries)
{
	struct microcode_filter_entry **pp;

	assert(base);

	/* both lists are sorted, no need to rescan from the
	 * beginning at every iteration */
	pp = base;
	while (entries) {
		struct microcode_filter_entry *e;

		e = entries;
		entries = entries->next;

		while (*pp && (*pp)->cpuid < e->cpuid)
			pp = &((*pp)->next);

		if (!*pp || (*pp)->cpuid != e->cpuid ||
		    !xx_merge_filter(*pp, e->pfm, e->rev, e->rev_match, e->invert)) {
			/* insert before */
			e->next = *pp;
			*pp = e;
			e = NULL;
		}
		free(e);
	}
}

static int xx_compare_rev(const int32_t rev,
			  const struct microcode_filter_entry *f)
{
	const enum iuc_rev_match_mode op = f->rev_match;

	if ((op == IUCODE_REVFLT_ANY) ||
	    (op == IUCODE_REVFLT_EQ && rev == f->rev) ||
	    (op == IUCODE_REVFLT_LT && rev < f->rev)  ||
	    (op == IUCODE_REVFLT_GT && rev > f->rev)) {
		return !(f->invert);
	}

	return -1;
}

/* returns non-zero if selected */
static int is_selected(const uint32_t cpuid,
		       const uint32_t pf_mask,
		       const int32_t rev,
		       const struct microcode_filter_entry *f)
{
	int state = -1;

	while (f && f->cpuid < cpuid)
		f = f->next;
	while (f && f->cpuid == cpuid && state < 0) {
		if ((pf_mask & f->pfm) != 0 ||
		    f->pfm == PFMASK_MATCH_ANY) {
			state = xx_compare_rev(rev, f);
		}
		f = f->next;
	}

	return (state >= 0)? state : filter_list_allow;
}

/* Microcode extended signature deduplication */

static int xx_xtsdeduplist_add(struct intel_uclist_entry * const e,
			       struct microcode_id_entry **list)
{
	struct microcode_id_entry *n;

	n = malloc(sizeof(struct microcode_id_entry));
	if (!n)
		return ENOMEM;

	n->id = e->uc;
	n->next = *list;
	*list = n;

	return 0;
}

/**
 * xtsdeduplist_check_and_add() - track mcu objects for dedup
 *
 * @e:    entry to add signature from to the list
 * @list: pointer to the head of the list used to track duplicates
 *
 * Duplicate tracking is done by storing and comparing struct
 * intel_uclist_entry->uc pointers.  This will work _only_ to dedup
 * several instances of the same microcode due to extended signature
 * processing.
 *
 * It cannot detect multiple copies of the same data stored in
 * multiple objects.
 *
 * To use:
 * Init the list head to NULL.  Call xtsdeduplist_check_and_add() for
 * every struct intel_uclist_entry element you want to track.
 *
 * One should use free_xtsdeduplist() to free the tracking list after
 * use.
 *
 * Returns:
 *   ENOMEM: cannot allocate memory to add entry to list
 *   EEXIST: entry is a duplicate
 *   0:      entry is not a duplicate
 */
static int xtsdeduplist_check_and_add(struct intel_uclist_entry * const e,
                                      struct microcode_id_entry **list)
{
	const void *id = e->uc;

	while (*list) {
		if ((*list)->id == id)
			return EEXIST;
		list = &((*list)->next);
	}
	return xx_xtsdeduplist_add(e, list);
}

/**
 * free_xtsdeduplist() - frees mcu dedup tracking list
 * @list:   head of the list to be freed
 *
 * Frees every element of the list.  @list will be invalid
 * after this function returns.
 */
static void free_xtsdeduplist(struct microcode_id_entry *list)
{
	struct microcode_id_entry *e;

	while (list) {
		e = list;
		list = list->next;
		free(e);
	}
}

static int uclist_annotate_extsig_dup(struct intel_uclist_entry * const uclist)
{
	struct microcode_id_entry *dlist = NULL;
	struct intel_uclist_entry *e;
	int rc = 0;

	if (!extsig_tables_in_use)
		return 0;

	e = uclist;
	while (e) {
		if (unlikely(e->flags & INTEL_UCLE_HASXST)) {
			rc = xtsdeduplist_check_and_add(e, &dlist);
			if (rc == EEXIST) {
				e->flags |= INTEL_UCLE_NOWR;
			} else if (!rc) {
				e->flags &= ~INTEL_UCLE_NOWR;
			} else {
				break;
			}
		}
		e = e->next;
	}

	free_xtsdeduplist(dlist);
	return (rc != EEXIST) ? rc : 0;
}

/* Microcode processing */

static int xx_uclist_add_print_errors(const int status)
{
	switch (status) {
	case EEXIST:
	case 0:
		return 0;
	case ENOMEM:
		print_err("Cannot add index entry: out of memory");
		return 1;
	case EINVAL:
		print_err("Internal error: uclist_merge_signature() returned EINVAL");
		return 1;
	default:
		return 1;
	}
}

static int xx_datefilter_loose_inplaceinsert(struct intel_uclist_entry ** const p,
					     struct intel_uclist_entry ** const uclist)
{
	const uint32_t cpuid = (*p)->cpuid;
	const uint32_t pf_mask = (*p)->pfm;
	struct intel_uclist_entry *e;
	int rc;

	e = *uclist;
	while (e && e->cpuid < cpuid)
		e = e->next;
	*uclist = e; /* speed up next search */

	while (e && e->cpuid == cpuid) {
		if (e->flags & INTEL_UCLE_SELID && e->pfm & pf_mask) {
			e->flags &= ~INTEL_UCLE_SELID;
			e->flags |= INTEL_UCLE_SELECT;
			rc = xx_uclist_add_print_errors(
			        uclist_merge_signature(e->id, e->gid,
				        e->flags, e->cpuid, e->pfm,
					e->uc_rev, e->uc, e->uc_size,
					allow_downgrade, p));
			if (rc)
				return rc;
		}
		e = e->next;
	}

	return 0;
}

static int check_downgrade_shadowing(const struct intel_uclist_entry * uclist)
{
	int res = 0;

	while (uclist) {
		const struct intel_uclist_entry *e = uclist->next;
		const uint32_t sig = uclist->cpuid;
		const uint32_t pfm = uclist->pfm;
		const int32_t  rev = uclist->uc_rev;
		const uint32_t gid = uclist->gid;

		while(e && e->cpuid == sig) {
			if ((pfm & e->pfm) != 0 &&
			    ((gid > e->gid && rev < e->uc_rev) ||
			     (gid < e->gid && rev > e->uc_rev))) {
				print_warn("cannot downgrade microcode sig 0x%x, "
					  "pf_mask 0x%02x (entries: " UCODE_ID_FMT_UU
					  " rev 0x%x, and " UCODE_ID_FMT_UU " rev 0x%x)",
					  sig, (pfm & e->pfm),
					  gid, uclist->id, (uint32_t) rev,
					  e->gid, e->id, (uint32_t) e->uc_rev);
				res = EEXIST;
			}
			e = e->next;
		}

		uclist = uclist->next;
	}

	return res;
}

static int xx_process_ucode_signature_cb(void * const userdata,
				const unsigned int sig_count,
				const uint32_t cpuid,
				const uint32_t pf_mask,
				__attribute__((unused)) const void * const uc_data,
				__attribute__((unused)) const unsigned int uc_data_size,
				const void * const uc,
				const unsigned int uc_size)
{
	struct microcode_iterator_data * const ctx = userdata;
	struct intel_ucode_metadata m;
	intel_ucode_status_t s;
	int add_status;
	uint32_t uce_flags = 0;

	assert(ctx);

	ctx->total_signature_count++;

	s = intel_ucode_getmetadata(uc, &m);
	if (s != INTEL_UCODE_NOERROR) {
		print_err("Microcode entry " UCODE_ID_FMT_UU ": %s",
			  ctx->current_bundle->id, ctx->current_uc,
			  intel_ucode_errstr(s));
		return 1;
	}

	if (m.extsig_count) {
		uce_flags = (sig_count) ?
			(INTEL_UCLE_HASXST | INTEL_UCLE_EXTSIG) :
			INTEL_UCLE_HASXST;
		extsig_tables_in_use = 1;
	}
	if (is_selected(cpuid, pf_mask, m.revision, uc_filter_list))
		uce_flags |= (is_in_date_range(&m)) ?
				INTEL_UCLE_SELECT : INTEL_UCLE_SELID;

	if (list_all_microcodes) {
		if (!sig_count)
			fprintf(stdout,
				"  " UCODE_ID_FMT_UU ": sig 0x%08x, pf_mask 0x%02x, "
				"%04x-%02x-%02x, rev 0x%04x, size %u\n",
				ctx->current_bundle->id, ctx->current_uc,
				cpuid, pf_mask,
				m.date_year, m.date_month, m.date_day,
				(uint32_t) m.revision, uc_size);
		else
			fprintf(stdout,
				"           sig 0x%08x, pf_mask 0x%02x, "
				"%04x-%02x-%02x, rev 0x%04x\n",
				cpuid, pf_mask,
				m.date_year, m.date_month, m.date_day,
				(uint32_t) m.revision);
	}

	add_status = uclist_add_signature(ctx->current_uc,
			ctx->current_bundle->id, uce_flags,
			cpuid, pf_mask, m.revision, uc, uc_size,
			strict_checks, &all_microcodes);

	switch (add_status) {
	case 0:
		ctx->total_unique_sig_count++;
		break;
	case EEXIST:
		break;
	case EBADF:
		print_warn("microcode " UCODE_ID_FMT_UU " has the same revision "
			  "and signature as a previously loaded microcode, but "
			  "different contents", ctx->current_bundle->id, ctx->current_uc);
		return 1;
	case EINVAL:
		print_err("Internal error: uclist_add_signature() returned EINVAL");
		return 1;
	default:
		print_err("Failed to add microcode entry " UCODE_ID_FMT_UU ": %s",
			  ctx->current_bundle->id, ctx->current_uc, strerror(add_status));
		return 1;
	}

	if (uce_flags & INTEL_UCLE_SELECT) {
		add_status = uclist_merge_signature(ctx->current_uc,
					ctx->current_bundle->id, uce_flags,
					cpuid, pf_mask, m.revision,
					uc, uc_size, allow_downgrade,
					&microcodes);
		if (add_status && add_status != EEXIST) {
			print_err("Failed to select microcode entry " UCODE_ID_FMT_UU ": %s",
				  ctx->current_bundle->id, ctx->current_uc,
				  strerror(add_status));
			return 1;
		}
	}

	return xx_uclist_add_print_errors(add_status);
}

static int xx_process_ucode_entry_cb(void * const userdata,
			         const unsigned int uc_count,
			         const void * const uc,
				 const size_t uc_max_size)
{
	struct microcode_iterator_data * const ctx = userdata;
	intel_ucode_status_t s;

	assert(ctx);

	ctx->current_uc = uc_count;

	s = intel_ucode_check_microcode(uc, uc_max_size, strict_checks);
	if (s != INTEL_UCODE_NOERROR) {
		print_err("microcode " UCODE_ID_FMT_UU ": %s",
			  ctx->current_bundle->id, ctx->current_uc,
			  intel_ucode_errstr(s));
		return (ignore_bad_ucode) ? 0 : 1;
	}

	ctx->total_entry_count++;

	s = intel_ucode_foreach_signature(uc, xx_process_ucode_signature_cb,
					  userdata);
	if (s != INTEL_UCODE_NOERROR && !ignore_bad_ucode) {
		print_err("aborting microcode processing...");
		return 1;
	}

	return 0;
}

static int do_process_microcodes(void)
{
	intel_ucode_status_t s;
	struct microcode_bundle *mcb;

	memset(&microcode_iterator_data, 0, sizeof(microcode_iterator_data));
	mcb = microcode_bundles;

	while (mcb) {
		if ((list_all_microcodes || list_sel_microcodes) && verbosity > 0)
			fprintf(stdout, "microcode bundle %u: %s\n", mcb->id,
				mcb->filename ? mcb->filename : "");

		microcode_iterator_data.current_bundle = mcb;
		s = intel_ucode_foreach_microcode(mcb->data, mcb->size,
						xx_process_ucode_entry_cb,
						&microcode_iterator_data);
		if (s != INTEL_UCODE_NOERROR) {
			if (s != INTEL_UCODE_CALLBACK_ERROR)
				print_err("microcode bundle %s: %s",
					mcb->filename ? mcb->filename : "(no filename)",
					intel_ucode_errstr(s));

			if (!ignore_bad_ucode)
				return 1;
		}

		mcb = mcb->next;
	}

	print_msg(2, "processed %lu valid microcode(s), %lu signature(s), %lu unique signature(s)",
		  microcode_iterator_data.total_entry_count,
		  microcode_iterator_data.total_signature_count,
		  microcode_iterator_data.total_unique_sig_count);

	/*
	 * we iterate over all selected microcodes only once, either
	 * to list selected microcodes, or to gather some stats when
	 * the verbosity is high, or to implement the loose date
	 * filtering mode.
	 */
	if (list_sel_microcodes || datefilter_loose || verbosity >= 2) {
		struct intel_uclist_entry *uce = microcodes;
		struct intel_uclist_entry *ucl = all_microcodes;
		struct microcode_id_entry *dl = NULL;
		unsigned long int uccount = 0;
		unsigned long int sigcount = 0;

		if (list_sel_microcodes)
			fprintf(stdout, "selected microcodes:\n");

		while (uce) {
			/*
			 * search any extra microcode for loose mode and
			 * insert it at this point
			 */
			if (unlikely(datefilter_loose &&
			    xx_datefilter_loose_inplaceinsert(&uce, &ucl)))
					return 1;

			/* note: we ignore ENOMEM results for performance */
			if (likely(!(uce->flags & INTEL_UCLE_HASXST) ||
				   !xtsdeduplist_check_and_add(uce, &dl)))
				uccount++;

			sigcount++;

			if (list_sel_microcodes) {
				struct intel_ucode_metadata m;

				if (unlikely(intel_ucode_getmetadata(uce->uc, &m) != INTEL_UCODE_NOERROR))
					exit(EXIT_SWFAILURE); /* memory corruption */
				fprintf(stdout,
					"  " UCODE_ID_FMT_UU ": sig 0x%08x, pf_mask 0x%02x, "
					"%04x-%02x-%02x, rev 0x%04x, size %u\n",
					uce->gid, uce->id, uce->cpuid, uce->pfm,
					m.date_year, m.date_month, m.date_day,
					(uint32_t) m.revision, uce->uc_size);
			}

			uce = uce->next;
		}

		free_xtsdeduplist(dl);
		print_msg(2, "selected %lu microcode(s), %lu signature(s)",
			  uccount, sigcount);
	}

	/* detect annoying downgrade mode shadows, and warn */
	if (allow_downgrade)
		check_downgrade_shadowing(microcodes);

	return 0;
}

static int do_write_microcode(const char * const filename, const int ft)
{
	int rc;

	if (!microcodes)
		return 0;

	print_msg(1, "Writing selected microcodes to: %s", filename);
	rc = write_intel_microcodes(AT_FDCWD, filename, ft, microcodes);
	if (rc == ENODATA) {
		print_warn("All microcodes in %s were skipped, file unchanged", filename);
		return 0;
	}
	return rc;
}

static int do_upload_microcode(const char * const filename)
{
	if (!microcodes)
		return 0;

	print_msg(1, "Uploading selected microcodes to: %s", filename);
	return upload_intel_microcodes(filename, microcodes);
}

static int do_write_named(const char * const dirname,
			  const struct intel_uclist_entry * const ucl)
{
	char fn[35]; /* "s%08X_m%08X_r%08X.fw" */
	int dirfd;
	struct stat st;

	struct intel_uclist_entry e;
	const struct intel_uclist_entry *p;
	unsigned long int count;
	int rc;

	if (!ucl)
		return 0;

	dirfd = open(dirname, O_RDONLY);
	if (dirfd == -1) {
		rc = errno;
		print_err("%s: cannot open: %s", dirname,
			  strerror(rc));
		return rc;
	}

	/* were we given a directory ? */
	if (fstat(dirfd, &st) == -1) {
		rc = errno;
		print_err("%s: cannot stat inode: %s", dirname,
			  strerror(rc));
		goto err_exit;
	}
	if (!S_ISDIR(st.st_mode)) {
		print_err("%s: is not a directory", dirname);
		rc = EINVAL;
		goto err_exit;
	}

	print_msg(1, "Writing microcode file(s) into %s", dirname);

	p = ucl;
	count = 0;
	rc = 0;

	while (p && !rc) {
		if (!(p->flags & INTEL_UCLE_DUPSIG)) {
			snprintf(fn, sizeof(fn), "s%08X_m%08X_r%08X.fw",
				     p->cpuid, p->pfm, (uint32_t) p->uc_rev);

			memcpy(&e, p, sizeof(e));
			e.next = NULL;
			e.flags &= ~INTEL_UCLE_NOWR;
			rc = write_intel_microcodes(dirfd, fn, 0, &e);
			if (!rc)
				count++;
		}

		p = p->next;
	}

	if (fsync(dirfd)) {
		rc = errno;
		print_err("%s: error while flushing directory inodes: %s",
			  dirname, strerror(rc));
	}

	if (count)
		print_msg(2, "%lu file(s) were written into %s", count, dirname);
	else
		print_msg(1, "no files were written into %s", dirname);

err_exit:
	close(dirfd);

	return rc;
}

static int do_write_firmware(const char * const dirname)
{
	char fn[35]; /* "%02x-%02x-%02x" */
	int dirfd;
	struct stat st;

	struct intel_uclist_entry *samecpuid_list, *p;
	unsigned long int count;
	int rc;

	if (!microcodes)
		return 0;

	dirfd = open(dirname, O_RDONLY);
	if (dirfd == -1) {
		rc = errno;
		print_err("%s: cannot open: %s", dirname,
			  strerror(rc));
		return rc;
	}

	/* were we given a directory ? */
	if (fstat(dirfd, &st) == -1) {
		rc = errno;
		print_err("%s: cannot stat inode: %s", dirname,
			  strerror(rc));
		goto err_exit;
	}
	if (!S_ISDIR(st.st_mode)) {
		print_err("%s: is not a directory", dirname);
		rc = EINVAL;
		goto err_exit;
	}

	print_msg(1, "Writing microcode firmware file(s) into %s", dirname);

	p = microcodes;
	samecpuid_list = NULL;
	count = 0;
	rc = 0;

	/* the lists are already ordered by cpuid */
	while (p && !rc) {
		uint32_t cpuid;
		unsigned int x86_family, x86_model, x86_mask;
		int add_status;

		/* select all that share the same cpuid */
		cpuid = p->cpuid;
		while (p && p->cpuid == cpuid) {
			add_status = uclist_merge_signature(p->id, p->gid,
						p->flags, p->cpuid, p->pfm,
						p->uc_rev, p->uc, p->uc_size,
						0, &samecpuid_list);
			if (xx_uclist_add_print_errors(add_status)) {
				rc = add_status;
				goto err_exit;
			}

			p = p->next;
		}

		/* write to file in dirname/ as expected by the kernel */

		x86_family = (cpuid >> 8) & 0x0f;
		x86_model  = (cpuid >> 4) & 0x0f;
		x86_mask   = cpuid & 0x0f;
		if (x86_family == 0x0f)
			x86_family += (cpuid >> 20) & 0xff;
		if (x86_family >= 0x06)
			x86_model += ((cpuid >> 16) & 0x0f) << 4;

		snprintf(fn, sizeof(fn), "%02x-%02x-%02x",
			     x86_family, x86_model, x86_mask);

		rc = uclist_annotate_extsig_dup(samecpuid_list);
		if (rc)
			goto err_exit;

		rc = write_intel_microcodes(dirfd, fn, 0, samecpuid_list);

		free_uclist(samecpuid_list);
		samecpuid_list = NULL;

		if (!rc)
			count++;
		if (rc == ENODATA)
			rc = 0;
	}

	if (fsync(dirfd)) {
		rc = errno;
		print_err("%s: error while flushing directory inodes: %s",
			  dirname, strerror(rc));
	}

	if (count)
		print_msg(2, "%lu file(s) were written into %s", count, dirname);
	else
		print_msg(1, "no files were written into %s", dirname);

err_exit:
	close(dirfd);

	return rc;
}

static int xx_scan_handle_sig(const uint32_t id1, const uint32_t id2,
			      const uint32_t id3, const uint32_t sig,
			      struct microcode_filter_entry ** const ucfp)
{
	/* Is it a supported Intel processor ? */
	if (id1 == 0x756e6547 && /* "Genu" */
	    id3 == 0x49656e69 && /* "ineI" */
	    id2 == 0x6c65746e) { /* "ntel" */
		/*
		 * Get main processor signature from cpuid(1) EAX
		 * and add it as a filter.
		 */
		switch (add_filter_to_list(sig, 0, 0, IUCODE_REVFLT_ANY, 0, ucfp)) {
		case ENOMEM:
			print_err("out of memory");
			return ENOMEM;
		case EEXIST:
			return 0;
		default:
			print_msg(1, "system has processor(s) with signature 0x%08x", sig);
			return 0;
		}
	}

	return ENXIO;
}

#ifdef USE_CPUID_DEVICE
static int is_base10_number(const char *s)
{
	if (!s || !*s)
		return 0;

	while(*s) {
		if (*s < '0' || *s > '9')
			return 0;
		s++;
	}

	return 1;
}

/* requires a kernel driver, and *really* hurts on very large systems */
static int xx_check_cpuid_devs(struct microcode_filter_entry ** const ucfp)
{
	uint32_t cpuid_buf[8];	/* two cpuid levels */
	char cpuid_device[PATH_MAX];
	int cpuid_fd;
	int rc = 0;
	unsigned int i = 0;
	unsigned int ncpu = 0;
	struct dirent *d;

	DIR *cpudir = opendir(CPUID_DEVICE_PARENT);
	if (!cpudir) {
		int en = errno;
		print_err("%s: could not open directory: %s", CPUID_DEVICE_PARENT, strerror(en));
		return -1;
	}

	while (1) {
		errno = 0;
		d = readdir(cpudir);
		if (!d) {
			int err = errno;
			if (unlikely(err)) {
				print_err("%s: cannot walk directory: %s",
					  CPUID_DEVICE_PARENT, strerror(err));
				rc = -1; /* note that we skipped processors due to unexpected errors */
			}
			break; /* finish/abort walk */
		}

		/* Linux procfs supports d_type */
		if (d->d_type != DT_DIR)
			continue; /* next dentry */

		/* must be [0-9]+, no trailling weirdness */
		if (!is_base10_number(d->d_name))
			continue; /* next dentry */

		snprintf(cpuid_device, sizeof(cpuid_device),
			 CPUID_DEVICE_BASE, d->d_name);

		errno = EINTR;
		cpuid_fd = -1;
		while (cpuid_fd == -1 && errno == EINTR)
			cpuid_fd = openat(dirfd(cpudir), cpuid_device, O_RDONLY | O_CLOEXEC);
		if (cpuid_fd == -1) {
			int en = errno;

			print_msg(4, "%s/%s: returned error status on open(): %s",
				  CPUID_DEVICE_PARENT, cpuid_device, strerror(en));

			if (en == ENOENT)
				break;    /* cpuid device inode not found */
			if (en == ENXIO || en == EIO) {
				/* Linux cpuid driver: ENXIO: offline; EIO: no cpuid support */
				print_msg(2, "processor %u is offline or has no cpuid support", i);
			} else {
				print_msg(2, "%s/%s: cannot open cpuid device node: %s",
					CPUID_DEVICE_PARENT, cpuid_device, strerror(en));
				rc = -1; /* note that we skipped processors due to unexpected errors */
			}

			/* skip this processor */
			i++;
			continue;
		}

		print_msg(3, "trying to get CPUID information from %s/%s",
			  CPUID_DEVICE_PARENT, cpuid_device);
		if (read(cpuid_fd, &cpuid_buf, sizeof(cpuid_buf)) == -1) {
			print_err("%s/%s: access to CPUID(0) and CPUID(1) failed: %s",
				  CPUID_DEVICE_PARENT, cpuid_device, strerror(errno));
			/* this is in the should not happen list, so abort */
			close(cpuid_fd);
			closedir(cpudir);
			return 1;
		}

		close(cpuid_fd);
		ncpu++;

		if (xx_scan_handle_sig(cpuid_buf[1], cpuid_buf[2],
				       cpuid_buf[3], cpuid_buf[4], ucfp) == ENOMEM) {
			rc = -1;
			break;
		}

		i++;
	};

	closedir(cpudir);

	if (i == 0 && ncpu == 0) {
		print_err("cpuid kernel driver unavailable");
		return -1;
	} else if (rc) {
		if (ncpu)
			print_err("some processors were not scanned due to unexpected errors");
		else
			print_err("could not open any cpuid devices");
	}

	if (ncpu)
		print_msg(2, "checked the signature of %u processor(s)", ncpu);

	return rc;
}
#else
static int xx_check_cpuid_devs(__attribute__((unused)) struct microcode_filter_entry ** const ucfp)
{
	print_msg(1, "support for exact system scan disabled at compile time");
	return -1;
}
#endif /* USE_CPUID_DEVICE */


/* xx_add_all_steppings(cpuid) hurts a lot less on very big systems... */
static int xx_add_all_steppings(uint32_t sig, struct microcode_filter_entry ** const ucfp)
{
	unsigned int i;

	sig |= 0xf;
	for (i = 0; i < 0x10; i++) {
		if (add_filter_to_list(sig, 0, 0, IUCODE_REVFLT_ANY, 0, ucfp) == ENOMEM) {
			print_err("out of memory");
			return ENOMEM;
		}
		sig--;
	}
	return 0;
}

/* Handle mixed-signature systems, even if Intel still hasn't admited that
 * they will exist in the SDM, the writing is already out in the wall */
static int xx_add_all_steppings_for_every_sig(uint32_t sig, struct microcode_filter_entry ** const ucfp)
{
	struct microcode_filter_entry *fl = NULL;
	struct microcode_filter_entry *p;
	int rc;

	/* failsafe of the failsafe: add running processor sig */
	rc = xx_add_all_steppings(sig, &fl);

	/* creating a new list is just plain safer in the long run */
	p = *ucfp;
	while (p && !rc) {
		/* handle mixed sig systems, unlikely as that might be */
		if (p->cpuid != sig)
			rc = xx_add_all_steppings(p->cpuid, &fl);
		p = p->next;
	}

	if (!rc) {
		free_filter_list(*ucfp);
		*ucfp = fl;
	} else {
		free_filter_list(fl);
	}

	return rc;
}

static int scan_system_processors(unsigned int strategy,
	                          struct microcode_filter_entry ** const filter_list)
{
	uint32_t id0, id1, id2, id3, sig, idx;
	struct microcode_filter_entry *uc_cpu = NULL;
	int rc = 0;

	assert(filter_list);

	print_msg(3, "trying to get CPUID information directly");
	if (!(__get_cpuid(0, &id0, &id1, &id2, &id3) &&
	      __get_cpuid(1, &sig, &idx, &idx, &idx))) {
		print_msg(1, "microcode signature unavailable");
		return 0;
	}

	/*
	 * fail-safe: only change filter_list_allow (switch away from "select
	 * all microcodes by default") if we did scan/cpuid.  This way, all
	 * microcodes will be included if cpuid is not available, and no other
	 * microcode selection option was used.  On non-Intel, this results in
	 * "no microcodes by default", because the scan/cpuid was successful,
	 * but uc_cpu will be empty.
	 */

	switch (xx_scan_handle_sig(id1, id2, id3, sig, &uc_cpu)) {
	case 0:
		if (strategy == 2) {
			if (xx_check_cpuid_devs(&uc_cpu)) {
				print_warn("exact cpuid signature scan failed, switching to failsafe strategy");
				rc = xx_add_all_steppings_for_every_sig(sig, &uc_cpu);
			}
		} else {
			print_msg(2, "assuming all processors have the same type, family and model");
			rc = xx_add_all_steppings(sig, &uc_cpu);
		}
		break;
	case ENXIO:
		print_msg(1, "running on a non-Intel processor");
		break;
	default:
		rc = 1;
	}

	if (!rc) {
		filter_list_allow = 0;
	} else {
		print_warn("cpuid signature scan failed, ignoring incomplete results...");
	}

	if (uc_cpu) {
		/* tie linked lists */
		add_filter_list_to_list(filter_list, uc_cpu);
		uc_cpu = NULL;
	}

	return (rc > 0) ? rc : 0;
}

static int cmdline_queue_ucode_filter(uint32_t cpuid, uint32_t pf_mask,
				      int32_t rev, enum iuc_rev_match_mode rev_match,
				      int invert)
{
	struct microcode_filter_entry *n;

	n = malloc(sizeof(struct microcode_filter_entry));
	if (!n)
		return ENOMEM;
	n->cpuid = cpuid;
	n->pfm = pf_mask;
	n->rev = rev;
	n->rev_match = rev_match;
	n->invert = invert;
	n->next = NULL;

	if (uc_filter_queue_tail) {
		uc_filter_queue_tail->next = n;
	} else {
		uc_filter_queue = n;
	}

	uc_filter_queue_tail = n;

	return 0;
}

static int process_ucode_filter_queue(void)
{
	struct microcode_filter_entry *p = uc_filter_queue;
	int rc = 0;

	while(p && !rc) {
		if (p->cpuid != IUCODE_FILTERMASK_SCANCPUS) {
			rc = add_filter_to_list(p->cpuid, p->pfm, p->rev, p->rev_match, p->invert, &uc_filter_list);
			if (rc == EEXIST)
				rc = 0;
		} else {
			rc = scan_system_processors(p->pfm, &uc_filter_list);
		}

		p = p->next;
	}

	uc_filter_queue_tail = NULL;
	free_filter_list(uc_filter_queue);
	uc_filter_queue = NULL;

	switch(rc) {
	case 0:
		return 0;

	case ENOMEM:
		print_err("Cannot add filter entry: out of memory");
		break;
	case EINVAL:
		print_err("Internal error while processing filter list");
		break;
	}

	return 1;
}

/* Command line processing */

static const char program_version[] =
	PROGNAME " " VERSION "\n"
	"Copyright (c) 2010-2018 by Henrique de Moraes Holschuh\n\n"

	"Based on code from the Linux microcode_intel driver and from\n"
	"the microcode.ctl package, copyright (c) 2000 by Simon Trimmer\n"
	"and Tigran Aivazian.\n\n"

	"This is free software; see the source for copying conditions.\n"
	"There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
	"A PARTICULAR PURPOSE.";
static const char program_bug_address[] = PACKAGE_BUGREPORT;

static const char cmdline_doc[] =
	PROGNAME " - Tool to manipulate Intel IA32/X86_64 microcode bundles\n"

	"\v"

	"The microcode bundle files should be specified as arguments.  "
	"The bundle type is determined by the file name suffix.  It "
	"defaults to the binary format.\n\n"

	"Should the filename end with \".bin\", binary mode will be "
	"used.  Should the filename end with \".dat\", text mode will be "
	"used.  The -t option can be used to set the type of the "
	"microcode bundle files that come after it, e.g. "
	"-td /tmp/dat-file -tb /tmp/binary /tmp/binary2.\n\n"

	"To load microcode data from stdin, use \"-\" as the filename.  "
	"File type will be assumed to be text (\".dat\"), use option -tb "
	"to load binary data from stdin.\n\n"

	"To load all files from a directory, specify the directory name.  "
	"It will not recurse into subdirectories, they will be skipped.\n\n"

	"Empty files and directories will be ignored, and will be skipped.";

enum {
	IUCODE_ARGP_STRICTCHK = 0x81,
	IUCODE_ARGP_NOSTRICTCHK,
	IUCODE_ARGP_IGNOREBROKEN,
	IUCODE_ARGP_NOIGNOREBROKEN,
	IUCODE_ARGP_UNLINK,
	IUCODE_ARGP_NOUNLINK,
	IUCODE_ARGP_DOWNGRADE,
	IUCODE_ARGP_NODOWNGRADE,
	IUCODE_ARGP_DATEBEFORE,
	IUCODE_ARGP_DATEAFTER,
	IUCODE_ARGP_DATEFSTRICT,
	IUCODE_ARGP_DATEFLOOSE,
	IUCODE_ARGP_SCANSYSTEMOPT,
	IUCODE_ARGP_EIRFS,
	IUCODE_ARGP_MINSIZE_EIRFS,
	IUCODE_ARGP_DFLSIZE_EIRFS,
	IUCODE_ARGP_WRITENAMEDALL,
};

static const struct argp_option cmdline_options[] = {
	{ NULL, 'h', NULL, 0, "Give this help list", -1 },

	{ "quiet",   'q', NULL, 0, "Quiet operation",                1 },
	{ "verbose", 'v', NULL, 0, "Verbose operation (cumulative)", 1 },

	{ NULL, 't', "type", 0,
	   "Sets input file type for the next microcode files. The type is "
	   "a single character: \"b\" (binary), \"d\" (Intel .dat), \"r\" "
	   "(search inside unknown binary data), or \"a\" (type will be "
	   "selected by filename suffix)",
	  10 },

	{ NULL, 's', "! | [!]signature[,[pf_mask][,[lt:|eq:|gt:]revision]]", 0,
	   "Select microcodes by the specified signature, processor "
	   "flags mask (pf_mask), and revision.  Optionally, prefix revision "
	   "with eq: (equal -- implied there is no prefix), lt: (less than) "
	   "or gt: (greater than).  "
	   "Specify more than once to select/unselect more microcodes.  "
	   "Prefix with ! to unselect microcodes.  "
	   "Use -s ! to disable the default behavior of selecting all "
	   "microcodes when no -s or -S filter is specified",
	  20 },
	{ NULL, 'S', NULL, 0,
	   "Same as --scan-system=auto",
	  21 },
	{ "scan-system", IUCODE_ARGP_SCANSYSTEMOPT, "mode", OPTION_ARG_OPTIONAL,
	   "Select microcodes based on the running system processor(s).  "
	   "Can be combined with the -s option, and can be used only once.  "
	   "Microcodes selected by --scan-system can be unselected by a "
	   "later -s !<signature> option.  The optional mode argument "
	   "selects the strategy: 0 or auto (default); 1 or fast (good for "
	   "most systems, including mixed-stepping); and 2 or exact (slow, "
	   "supports multi-signature systems, requires the cpuid kernel "
	   "driver and might require root access)",
	  22 },

	{ "downgrade", IUCODE_ARGP_DOWNGRADE, NULL, 0,
	   "Instead of discarding microcodes based on revision level, "
	   "keep the one from the file loaded last.  Files are loaded "
	   "in the order they were specified in the command line",
	  25 },
	{ "no-downgrade", IUCODE_ARGP_NODOWNGRADE, NULL, 0,
	   "Keep the microcode with the highest revision level, regardless "
	   "of the file load order (default)",
	  25 },

	{ "date-before", IUCODE_ARGP_DATEBEFORE, "YYYY-MM-DD", 0,
	   "Select only microcodes older than the specified date",
	  27 },
	{ "date-after", IUCODE_ARGP_DATEAFTER, "YYYY-MM-DD", 0,
	   "Select only microcodes newer than the specified date",
	  27 },
	{ "loose-date-filtering", IUCODE_ARGP_DATEFLOOSE, NULL, 0,
	   "Consider for selection other revisions (outside of the date range) "
	   "of every microcode that was selected within the date range",
	  28 },
	{ "strict-date-filtering", IUCODE_ARGP_DATEFSTRICT, NULL, 0,
	   "Select only microcodes strictly within the date range (default)",
	  28 },

	{ "list",     'l', NULL, 0, "List selected microcode signatures", 30 },
	{ "list-all", 'L', NULL, 0, "List all microcode signatures",      30 },

	{ "kernel", 'k', "device", OPTION_ARG_OPTIONAL,
	   "Upload selected microcodes to the kernel.  Optionally, the "
	   "device path can be specified (default: " MICROCODE_DEVICE_DEFAULT
	   ")",
	  40 },
	{ "write-firmware", 'K', "directory", OPTION_ARG_OPTIONAL,
	   "Write selected microcodes with the filenames expected by the "
	   "Linux kernel firmware loader.  Optionally, the destination "
	   "directory can be specified (default: " MICROCODE_DIR_DEFAULT ")",
	  40 },
	{ "write-to", 'w', "file", 0,
	   "Write selected microcodes to a file in binary format.  "
	   "The binary format is suitable to be uploaded to the kernel",
	  40 },
	{ "write-earlyfw", IUCODE_ARGP_EIRFS, "file", 0,
	   "Write selected microcodes to an early initramfs file, which "
	   "should be prepended to the regular initramfs",
	  40 },
	{ "write-named-to", 'W', "directory", 0,
	   "Write selected microcodes to files in the specified directory, "
	   "in binary format.  The file name will reflect the microcode "
	   "signature, mask and revision",
	  41 },
	{ "write-all-named-to", IUCODE_ARGP_WRITENAMEDALL, "directory", 0,
	   "Write every microcode to files in the specified directory, "
	   "in binary format.  The file name will reflect the microcode "
	   "signature, mask and revision.  This is the only way to write "
	   "out every revision of a microcode",
	  42 },

	{ "overwrite", IUCODE_ARGP_UNLINK, NULL, 0,
	   "Unlink (remove) destination files before writing",
	  45 },
	{ "no-overwrite", IUCODE_ARGP_NOUNLINK, NULL, 0,
	   "Do not remove existing files (default)",
	  45 },

	{ "mini-earlyfw", IUCODE_ARGP_MINSIZE_EIRFS, NULL, 0,
	  "Optimize the early initramfs cpio container for minimal size.  "
	  "The microcode data file will not be available to the regular "
	  "initramfs, and tools might complain about the non-standard "
	  "cpio block size",
	  48 },
	{ "normal-earlyfw", IUCODE_ARGP_DFLSIZE_EIRFS, NULL, 0,
	  "Write a normal-sized early initramfs (default)",
	  48 },

	{ "strict-checks", IUCODE_ARGP_STRICTCHK, NULL, 0,
	   "Perform strict checks on the microcode data (default)",
	  50 },
	{ "no-strict-checks", IUCODE_ARGP_NOSTRICTCHK, NULL, 0,
	   "Perform less strict checks on the microcode data",
	  51 },

	{ "ignore-broken", IUCODE_ARGP_IGNOREBROKEN, NULL, 0,
	   "Skip broken microcode entries instead of aborting",
	  55 },
	{ "no-ignore-broken", IUCODE_ARGP_NOIGNOREBROKEN, NULL, 0,
	   "Abort on broken microcode entries (default)",
	  56 },

	{ .name = NULL },
};
static const char cmdline_nonarg_doc[] = "[[-t<type>] filename] ...";

static const char * const cmdline_scan_system_tbl[] = { "auto", "fast", "exact", NULL };

static int new_filename(const char * const fn,
			const intel_ucode_file_type_t ftype)
{
	struct filename_list **p, *n;
	size_t s, l;

	l = strlen(fn);
	if (l > 1 && fn[l-1] == '/')
		l--;

	if (!l)
		return EINVAL;

	s = sizeof(struct filename_list) + l + 1;
	n = malloc(s);
	if (!n)
		return ENOMEM;

	memset(n, 0, s);
	memcpy(n->path, fn, l);
	n->type = ftype;

	/* tail-add */
	p = &input_files;
	while (*p)
		p = &((*p)->next);
	*p = n;

	return 0;
}

static void free_filename_list(void) __attribute__((unused));
static void free_filename_list(void)
{
	struct filename_list *p, *q;

	p = input_files;

	while (p) {
		q = p;
		p = p->next;
		free(q);
	}
	input_files = NULL;
}

/* -s ! | [!]cpuid[,[pf_mask][,[<revmatch_operator>]rev]] */
static int cmdline_parse_ucode_filter(const char *arg)
{
	char *p;
	uint32_t acpuid, amask;
	int32_t arev;
	int invert, arev_match;

	amask = 0;
	invert = 0;
	arev = 0;

	while (isspace(*arg))
		arg++;
	if (*arg == '!') {
		invert = 1;
		arg++;

		/* handle -s ! */
		if (isspace(*arg)) {
			while (isspace(*arg))
				arg++;
			if (! *arg)
			    	return EINVAL;
		}
		if (! *arg) {
			filter_list_allow = 0;
			return 0;
		}
	}

	/* cpuid, mandatory */
	if (!*arg || parse_u32(arg, &p, 0, &acpuid))
		return EINVAL;
	while (isspace(*p))
		p++;

	/* pf_mask, optional */
	if (*p == ',') {
		p++;
		if (*p != ',') {
			arg = p;
			if (!*arg || parse_u32(arg, &p, 0, &amask))
				return EINVAL;
		} /* else amask = MATCH_ANY */
		while (isspace(*p))
			p++;
	} else if (*p) {
		return EINVAL;
	}

	/* rev, optional */
	if (*p == ',') {
		p++;
		while (isspace(*p))
			p++;
		if (!*p)
			return EINVAL;

		/* revison match operator, optional */
		int i = 0;
		while (i < IUCODE_REVFLT_SIZE &&
		       (!iuc_rev_match_mode_s[i] ||
		        strncasecmp(p, iuc_rev_match_mode_s[i],
			            strlen(iuc_rev_match_mode_s[i]))))
			i++;
		if (i < IUCODE_REVFLT_SIZE) {
			arev_match = i;
			p += strlen(iuc_rev_match_mode_s[i]);
		} else {
			arev_match = IUCODE_REVFLT_EQ;
		}

		/* revision */
		arg = p;
		if (!*arg || parse_s32e(arg, &p, 0, &arev))
			return EINVAL;
		while (isspace(*p))
			p++;
	} else {
		arev_match = IUCODE_REVFLT_ANY;
	}
	if (*p) {
		return EINVAL;
	}

	if (!invert)
		filter_list_allow = 0;

	return cmdline_queue_ucode_filter(acpuid, amask, arev, arev_match, invert);
}

/* YYYY-MM-DD */
static int cmdline_get_date(const char *arg, uint32_t *d)
{
	unsigned long int year, month, day;
	char *p;

	errno = 0;
	year = strtoul(arg, &p, 10);
	if (errno || p == arg || *p != '-')
		return EINVAL;
	arg = ++p;
	month = strtoul(arg, &p, 10);
	if (errno || p == arg || *p != '-')
		return EINVAL;
	arg = ++p;
	day = strtoul(arg, &p, 10);
	if (errno || p == arg)
		return EINVAL;
	while (isspace(*p))
		p++;
	if (*p)
		return EINVAL;

	if (year > 9999 || month > 99 || day > 99)
		return EINVAL;

	/* Encode in BCD: YYYYMMDD */
	*d = (year / 1000)     << 28 |
	     (year / 100 % 10) << 24 |
	     (year / 10 % 10)  << 20 |
	     (year % 10)       << 16 |
	     (month / 10)      << 12 |
	     (month % 10)      << 8 |
	     (day / 10)        << 4 |
	     (day % 10);

	return 0;
}

static int cmdline_get_int(const char *arg, int pmin, int pmax, int *d)
{
	char *p;
	long int l;

	if (!arg)
		return 0;

	errno = 0;
	l = strtol(arg, &p, 10);
	if (errno || p == arg)
		return errno ? errno : EINVAL;
	if (l > INT32_MAX || l < INT32_MIN)
		return ERANGE;
	if (l < pmin || l > pmax)
		return EINVAL;

	while (isspace(*p))
		p++;
	if (*p)
		return EINVAL;

	if (d)
		*d = (int) l;

	return 0;
}

/* note: does not tolerate trailing blanks */
static int cmdline_get_enumstr(const char *arg, const char * const table[],
			       int *d)
{
	int r = 0;

	if (!arg || !table)
		return 0;

	while (table[r]) {
		if (!strcasecmp(arg, table[r])) {
			if (d)
				*d = r;
			return 0;
		}
		r++;
	}

	return ENOENT;
}

static error_t cmdline_do_parse_arg(int key, char *arg,
				    struct argp_state *state)
{
	int rc;

	switch (key) {
	case 'h':
		argp_state_help(state, stdout, ARGP_HELP_STD_HELP);
		break; /* usually not reached */

	case 'q':
		verbosity = 0;
		break;
	case 'v':
		if (verbosity < 5)
			verbosity++;
		break;

	case 'L':
		list_all_microcodes = 1;
		break;
	case 'l':
		list_sel_microcodes = 1;
		break;

	case 't':
		if (strlen(arg) > 1)
			argp_error(state, "unknown file type: '%s'", arg);
		switch (*arg) {
		case 'd': /* .dat */
			ucfiletype = INTEL_UC_FT_DAT;
			break;
		case 'b': /* .bin */
			ucfiletype = INTEL_UC_FT_BIN;
			break;
		case 'r': /* search inside binary data */
			ucfiletype = INTEL_UC_FT_SCAN;
			break;
		case 'a': /* any (detect) */
			ucfiletype = INTEL_UC_FT_UNKNOWN;
			break;
		default:
			argp_error(state, "unknown file type: '%c'", *arg);
		}
		break;

	case 'k':
		if (command_line_actions & IUCODE_DO_UPLOADUC)
			argp_error(state,
				   "-k option can be specified only once");

		if (arg)
			upload_microcode = strdup(arg);
		else
			upload_microcode = strdup(MICROCODE_DEVICE_DEFAULT);

		command_line_actions |= IUCODE_DO_UPLOADUC;
		break;
	case 'K':
		if (command_line_actions & IUCODE_DO_WRITEFW)
			argp_error(state,
				   "-K option can be specified only once");

		if (arg)
			write_firmware = strdup(arg);
		else
			write_firmware = strdup(MICROCODE_DIR_DEFAULT);

		command_line_actions |= IUCODE_DO_WRITEFW;
		break;
	case 'w':
		if (command_line_actions & IUCODE_DO_WRITEUC)
			argp_error(state,
				   "-w option can be specified only once");

		write_microcode = strdup(arg);
		command_line_actions |= IUCODE_DO_WRITEUC;
		break;
	case IUCODE_ARGP_EIRFS:
		if (command_line_actions & IUCODE_DO_WRITEFWE)
			argp_error(state,
				   "--write-earlyfw option can be specified only once");

		write_early_firmware = strdup(arg);
		command_line_actions |= IUCODE_DO_WRITEFWE;
		break;
	case 'W':
		if (command_line_actions & IUCODE_DO_WRITEFWN)
			argp_error(state,
				   "-W option can be specified only once");

		write_named = strdup(arg);

		command_line_actions |= IUCODE_DO_WRITEFWN;
		break;
	case IUCODE_ARGP_WRITENAMEDALL:
		if (command_line_actions & IUCODE_DO_WRITFWNA)
			argp_error(state,
				   "--write-all-named-to option can be specified only once");

		write_named_all = strdup(arg);

		command_line_actions |= IUCODE_DO_WRITFWNA;
		break;


	case IUCODE_ARGP_UNLINK:
		unlink_files = 1;
		break;
	case IUCODE_ARGP_NOUNLINK:
		unlink_files = 0;
		break;

	case IUCODE_ARGP_MINSIZE_EIRFS:
		cpio_blocksize = 16; /* minimum sane alignment for x86 */
		cpio_parentdirs = 0; /* cut down lots of headers */
		break;
	case IUCODE_ARGP_DFLSIZE_EIRFS:
		cpio_blocksize = LINUX_CPIO_BLK_SIZE;
		cpio_parentdirs = 1;
		break;

	case 's':
		rc = cmdline_parse_ucode_filter(arg);
		switch (rc) {
		case 0:
			break; /* success */
		case EINVAL:
			argp_error(state, "invalid filter: '%s'", arg);
			break; /* not reached */
		default:
			argp_failure(state, EXIT_SWFAILURE, rc,
				     "could not queue filter '%s'", arg);
		}
		command_line_actions |= IUCODE_F_UCSELECT;
		break;

	case 'S':
		/*
		 * -S and --scan-system cannot be handled the same way
		 * because -S cannot be made OPTION_ARG_OPTIONAL: it would
		 * break command-line backwards compatibility when people
		 * specify several short options together and -S is not the
		 * last option in the chain
		 */
		if (command_line_actions & IUCODE_DO_SELPROC)
			argp_error(state,
				   "--scan-system option can be specified only once");
		/*
		 * note: the pfm field of the queue filter item is used to
		 * encode the scan-system strategy.
		 */
		rc = cmdline_queue_ucode_filter(IUCODE_FILTERMASK_SCANCPUS, 0, 0, 0, 0);
		if (rc)
			argp_failure(state, EXIT_SWFAILURE, rc,
				     "could not queue --scan-system action");
		command_line_actions |= IUCODE_DO_SELPROC | IUCODE_F_UCSELECT;
		break;
	case IUCODE_ARGP_SCANSYSTEMOPT:
		if (command_line_actions & IUCODE_DO_SELPROC)
			argp_error(state,
				   "--scan-system option can be specified only once");

		int scan_system_strategy = 0;
		if (cmdline_get_int(arg, 0, 2, &scan_system_strategy) &&
		    cmdline_get_enumstr(arg, cmdline_scan_system_tbl, &scan_system_strategy))
			argp_error(state, "invalid --scan-system mode: '%s'", arg);

		/*
		 * note: the pfm field of the queue filter item is used to
		 * encode the scan-system strategy.
		 */
		rc = cmdline_queue_ucode_filter(IUCODE_FILTERMASK_SCANCPUS,
		                                (uint32_t)scan_system_strategy,
						0, 0, 0);
		if (rc)
			argp_failure(state, EXIT_SWFAILURE, rc,
				     "could not queue --scan-system action");
		command_line_actions |= IUCODE_DO_SELPROC | IUCODE_F_UCSELECT;
		break;

	case IUCODE_ARGP_DATEBEFORE:
	case IUCODE_ARGP_DATEAFTER:
		if (cmdline_get_date(arg, (key == IUCODE_ARGP_DATEBEFORE) ?
					   &datefilter_max : &datefilter_min))
			argp_error(state, "invalid date: '%s'", arg);
		command_line_actions |= IUCODE_F_UCSELECT;
		break;
	case IUCODE_ARGP_DATEFSTRICT:
		datefilter_loose = 0;
		break;
	case IUCODE_ARGP_DATEFLOOSE:
		datefilter_loose = 1;
		break;

	case IUCODE_ARGP_STRICTCHK:
		strict_checks = 1;
		break;
	case IUCODE_ARGP_NOSTRICTCHK:
		strict_checks = 0;
		break;
	case IUCODE_ARGP_IGNOREBROKEN:
		ignore_bad_ucode = 1;
		break;
	case IUCODE_ARGP_NOIGNOREBROKEN:
		ignore_bad_ucode = 0;
		break;
	case IUCODE_ARGP_DOWNGRADE:
		allow_downgrade = 1;
		break;
	case IUCODE_ARGP_NODOWNGRADE:
		allow_downgrade = 0;
		break;

	case ARGP_KEY_ARG: /* NON-OPTION ARGUMENTS */
		rc = new_filename(arg, ucfiletype);
		if (rc)
			argp_failure(state, EXIT_SWFAILURE, rc,
				     "could not add path '%s'", arg);
		command_line_actions |= IUCODE_DO_LOADFILE;
		break;

	default:
		return ARGP_ERR_UNKNOWN;
	}

	return 0;
}

static struct argp cmdline_argp = {
	.options  = cmdline_options,
	.parser   = cmdline_do_parse_arg,
	.args_doc = cmdline_nonarg_doc,
	.doc      = cmdline_doc };

int main(int argc, char *argv[])
{
	int rc;

	progname = argv[0];

	sanitize_std_fds();

	argp_err_exit_status = EXIT_USAGE;
	argp_program_version = program_version;
	argp_program_bug_address = program_bug_address;
	argp_parse(&cmdline_argp, argc, argv, ARGP_IN_ORDER, NULL, NULL);

	if (!command_line_actions) {
		print_msg(1, "nothing to do...");
		goto do_nothing;
	}

	if (process_ucode_filter_queue()) {
		rc = EXIT_SWFAILURE;
		goto err_exit;
	}

	if (command_line_actions & IUCODE_DO_LOADFILE) {
		struct filename_list *fn = input_files;
		while (fn && next_bundle_id) {
			switch (load_intel_microcode(fn->path, fn->type)) {
			case 0:
				break;
			case ENOTSUP:
				rc = EXIT_USAGE;
				goto err_exit;
			default:
				if (!ignore_bad_ucode) {
					rc = EXIT_SWFAILURE;
					goto err_exit;
				}
			}
			fn = fn->next;
		}
		if (!next_bundle_id) {
			/* too many bundles: gid overflow */
			print_err("too many data files");
			rc = EXIT_SWFAILURE;
			goto err_exit;
		}

		if (microcode_bundles && do_process_microcodes()) {
			rc = EXIT_SWFAILURE;
			goto err_exit;
		}
	}

	/* flush --list-* output if it is still pending */
	fflush(stdout);

	if (command_line_actions & IUCODE_DOMASK_NEEDSUC) {
		if (microcodes) {
			rc = uclist_annotate_extsig_dup(microcodes);
			if (!rc && upload_microcode)
				rc = do_upload_microcode(upload_microcode);
			if (!rc && write_microcode)
				rc = do_write_microcode(write_microcode, 0);
			if (!rc && write_early_firmware)
				rc = do_write_microcode(write_early_firmware, 1);
			if (!rc && write_firmware)
				rc = do_write_firmware(write_firmware);
			if (!rc && write_named)
				rc = do_write_named(write_named, microcodes);
			if (!rc && write_named_all)
				rc = do_write_named(write_named_all, all_microcodes);

			if (rc) {
				rc = EXIT_SWFAILURE;
				goto err_exit;
			}
		} else {
			if (filter_list_active()) {
				print_msg(1, "No valid microcodes were selected, nothing to do...");
			} else {
				print_msg(1, "No valid microcodes were loaded, nothing to do...");
			}
		}
	}

do_nothing:
	rc = 0;
err_exit:

	/* Disable all of this cleanup for some speedup... */
#ifdef VALGRIND_BUILD
	free(write_microcode);
	free(upload_microcode);
	free(write_firmware);
	free(write_early_firmware);
	free(write_named);
	free(write_named_all);

	free_filter_list(uc_filter_list);
	uc_filter_list = NULL;
	free_uclist(microcodes);
	microcodes = NULL;
	free_uclist(all_microcodes);
	all_microcodes = NULL;
	free_intel_microcode_bundles();
	free_filename_list();
#endif /* VALGRIND_BUILD */

	return rc;
}