Ravenports generated: 22 Nov 2019 12:03
[ravenports.git] / bucket_CB / devcpu-data
1 # Buildsheet autogenerated by ravenadm tool -- Do not edit.
2
3 NAMEBASE=               devcpu-data
4 VERSION=                1.27
5 KEYWORDS=               sysutils
6 VARIANTS=               standard
7 SDESC[standard]=        Intel and AMD CPUs microcode updates
8 HOMEPAGE=               none
9 CONTACT=                nobody
10
11 DOWNLOAD_GROUPS=        main amd
12 SITES[main]=            GITHUB/intel:Intel-Linux-Processor-Microcode-Data-Files:microcode-20191115
13 SITES[amd]=             FREELOCAL/sbruno
14 DISTFILE[1]=            generated:main
15 DISTFILE[2]=            amd64-microcode.20191021.tar.xz:amd
16 DF_INDEX=               1 2
17 SPKGS[standard]=        single
18
19 OPTIONS_AVAILABLE=      none
20 OPTIONS_STANDARD=       none
21
22 ONLY_FOR_OPSYS=         freebsd dragonfly
23 NOT_FOR_ARCH=           aarch64
24
25 EXTRACT_DIRTY=          2
26
27 LICENSE=                CUSTOM1:single
28 LICENSE_NAME=           CUSTOM1:"Intel Software License Agreement"
29 LICENSE_FILE=           CUSTOM1:{{FILESDIR}}/LICENSE
30 LICENSE_SCHEME=         solo
31
32 FPC_EQUIVALENT=         sysutils/devcpu-data
33
34 RC_SUBR=                microcode_update:single
35
36 post-extract:
37         ${INSTALL_DATA} ${FILESDIR}/ucode-split.c ${WRKSRC}
38         ${SED} -e 's|%%GH_TAGNAME%%|microcode-20191115|' ${FILESDIR}/Makefile \
39                 > ${WRKSRC}/Makefile
40         ${CAT} ${WRKSRC}/intel-ucode/* ${WRKSRC}/intel-ucode-with-caveats/* \
41                 > ${WRKSRC}/intel-ucode.bin
42
43 do-install:
44         ${MKDIR} ${STAGEDIR}${PREFIX}/share/cpucontrol \
45                 ${STAGEDIR}/boot/firmware
46         ${INSTALL_DATA} ${WRKSRC}/mcodes/* ${STAGEDIR}${PREFIX}/share/cpucontrol
47         (cd ${WRKDIR}/${NAMEBASE}_2 && \
48             ${INSTALL_DATA} microcode_amd.bin microcode_amd_fam1[567]h.bin \
49             ${STAGEDIR}${PREFIX}/share/cpucontrol)
50         ${INSTALL_DATA} ${WRKSRC}/intel-ucode.bin ${STAGEDIR}/boot/firmware
51
52 [FILE:154:descriptions/desc.single]
53 This port supplies microcode updates for use with cpuctl(4) microcode
54 update facility.  These could be used to keep your processor's firmware
55 up-to-date.
56
57
58 [FILE:263:distinfo]
59 14b26d27be70774948b8cb582e298f5317263b8b8bb1fe8e41260eae54f531dc      2998063 intel-Intel-Linux-Processor-Microcode-Data-Files-microcode-20191115.tar.gz
60 077426150f52964e829fa31ddd5866e7b902746bea3fcd339b8d5cb761c1039a        22232 amd64-microcode.20191021.tar.xz
61
62
63 [FILE:2577:manifests/plist.single]
64 /boot/firmware/intel-ucode.bin
65 share/cpucontrol/
66  06-03-02.00
67  06-05-00.01
68  06-05-00.02
69  06-05-00.08
70  06-05-01.01
71  06-05-02.01
72  06-05-02.02
73  06-05-02.04
74  06-05-03.01
75  06-05-03.02
76  06-05-03.04
77  06-05-03.08
78  06-06-00.01
79  06-06-05.10
80  06-06-0a.02
81  06-06-0a.08
82  06-06-0a.20
83  06-06-0d.02
84  06-06-0d.08
85  06-06-0d.20
86  06-07-01.04
87  06-07-02.04
88  06-07-03.04
89  06-08-01.01
90  06-08-01.04
91  06-08-01.08
92  06-08-01.10
93  06-08-01.20
94  06-08-03.08
95  06-08-03.20
96  06-08-06.01
97  06-08-06.02
98  06-08-06.04
99  06-08-06.10
100  06-08-06.80
101  06-08-0a.10
102  06-08-0a.20
103  06-08-0a.80
104  06-09-05.10
105  06-09-05.20
106  06-09-05.80
107  06-0a-00.04
108  06-0a-01.04
109  06-0b-01.10
110  06-0b-01.20
111  06-0b-04.10
112  06-0b-04.20
113  06-0d-06.20
114  06-0e-08.20
115  06-0e-0c.20
116  06-0e-0c.80
117  06-0f-02.01
118  06-0f-02.20
119  06-0f-06.01
120  06-0f-06.04
121  06-0f-06.20
122  06-0f-07.10
123  06-0f-07.40
124  06-0f-0a.80
125  06-0f-0b.01
126  06-0f-0b.04
127  06-0f-0b.08
128  06-0f-0b.10
129  06-0f-0b.20
130  06-0f-0b.40
131  06-0f-0b.80
132  06-0f-0d.01
133  06-0f-0d.20
134  06-0f-0d.80
135  06-16-01.01
136  06-16-01.02
137  06-16-01.80
138  06-17-06.01
139  06-17-06.04
140  06-17-06.10
141  06-17-06.40
142  06-17-06.80
143  06-17-07.10
144  06-17-0a.11
145  06-17-0a.44
146  06-17-0a.a0
147  06-1a-04.03
148  06-1a-05.03
149  06-1c-02.01
150  06-1c-02.04
151  06-1c-02.08
152  06-1c-0a.01
153  06-1c-0a.04
154  06-1c-0a.08
155  06-1c-0a.10
156  06-1d-01.08
157  06-1e-05.13
158  06-25-02.12
159  06-25-05.92
160  06-26-01.01
161  06-26-01.02
162  06-2a-07.12
163  06-2c-02.03
164  06-2d-06.6d
165  06-2d-07.6d
166  06-2e-06.04
167  06-2f-02.05
168  06-37-08.02
169  06-37-08.0c
170  06-37-09.0f
171  06-3a-09.12
172  06-3c-03.32
173  06-3d-04.c0
174  06-3e-04.ed
175  06-3e-06.ed
176  06-3e-07.ed
177  06-3f-02.6f
178  06-3f-04.80
179  06-45-01.72
180  06-46-01.32
181  06-47-01.22
182  06-4c-03.01
183  06-4c-04.01
184  06-4d-08.01
185  06-4e-03.c0
186  06-55-03.97
187  06-55-04.b7
188  06-55-06.bf
189  06-55-07.bf
190  06-56-02.10
191  06-56-03.10
192  06-56-04.10
193  06-56-05.10
194  06-5c-02.01
195  06-5c-09.03
196  06-5c-0a.03
197  06-5e-03.36
198  06-5f-01.01
199  06-66-03.80
200  06-7a-01.01
201  06-7a-08.01
202  06-7e-05.80
203  06-8e-09.10
204  06-8e-09.c0
205  06-8e-0a.c0
206  06-8e-0b.d0
207  06-8e-0c.94
208  06-9e-09.2a
209  06-9e-0a.22
210  06-9e-0b.02
211  06-9e-0c.22
212  06-9e-0d.22
213  06-a6-00.80
214  0f-00-07.01
215  0f-00-07.02
216  0f-00-0a.01
217  0f-00-0a.02
218  0f-00-0a.04
219  0f-01-02.04
220  0f-02-04.02
221  0f-02-04.04
222  0f-02-04.10
223  0f-02-05.01
224  0f-02-05.02
225  0f-02-05.04
226  0f-02-05.10
227  0f-02-06.02
228  0f-02-07.02
229  0f-02-07.04
230  0f-02-07.08
231  0f-02-09.02
232  0f-02-09.04
233  0f-02-09.08
234  0f-03-02.0d
235  0f-03-03.0d
236  0f-03-04.1d
237  0f-04-01.02
238  0f-04-01.bd
239  0f-04-03.9d
240  0f-04-04.9d
241  0f-04-07.9d
242  0f-04-08.01
243  0f-04-08.02
244  0f-04-08.5f
245  0f-04-09.bd
246  0f-04-0a.5c
247  0f-04-0a.5d
248  0f-06-02.04
249  0f-06-04.01
250  0f-06-04.34
251  0f-06-05.01
252  0f-06-08.22
253  microcode_amd.bin
254  microcode_amd_fam15h.bin
255  microcode_amd_fam16h.bin
256  microcode_amd_fam17h.bin
257
258
259 [FILE:7134:files/LICENSE]
260 The terms of the software license agreement included with any software you download will control your use of the software.
261
262 INTEL SOFTWARE LICENSE AGREEMENT
263
264 IMPORTANT - READ BEFORE COPYING, INSTALLING OR USING.
265 Do not use or load this software and any associated materials (collectively,
266 the "Software") until you have carefully read the following terms and
267 conditions. By loading or using the Software, you agree to the terms of this
268 Agreement. If you do not wish to so agree, do not install or use the Software.
269
270 LICENSES: Please Note:
271 - If you are a network administrator, the "Site License" below shall
272 apply to you.
273 - If you are an end user, the "Single User License" shall apply to you.
274 - If you are an original equipment manufacturer (OEM), the "OEM License"
275 shall apply to you.
276
277 SITE LICENSE. You may copy the Software onto your organization's computers
278 for your organization's use, and you may make a reasonable number of
279 back-up copies of the Software, subject to these conditions:
280
281 1. This Software is licensed for use only in conjunction with Intel
282 component products. Use of the Software in conjunction with non-Intel
283 component products is not licensed hereunder.
284 2. You may not copy, modify, rent, sell, distribute or transfer any part
285 of the Software except as provided in this Agreement, and you agree to
286 prevent unauthorized copying of the Software.
287 3. You may not reverse engineer, decompile, or disassemble the Software.
288 4. You may not sublicense or permit simultaneous use of the Software by
289 more than one user.
290 5. The Software may include portions offered on terms in addition to those
291 set out here, as set out in a license accompanying those portions.
292
293 SINGLE USER LICENSE. You may copy the Software onto a single computer for
294 your personal, noncommercial use, and you may make one back-up copy of the
295 Software, subject to these conditions:
296
297 1. This Software is licensed for use only in conjunction with Intel
298 component products. Use of the Software in conjunction with non-Intel
299 component products is not licensed hereunder.
300 2. You may not copy, modify, rent, sell, distribute or transfer any part
301 of the Software except as provided in this Agreement, and you agree to
302 prevent unauthorized copying of the Software.
303 3. You may not reverse engineer, decompile, or disassemble the Software.
304 4. You may not sublicense or permit simultaneous use of the Software by
305 more than one user.
306 5. The Software may include portions offered on terms in addition to those
307 set out here, as set out in a license accompanying those portions.
308
309 OEM LICENSE: You may reproduce and distribute the Software only as an
310 integral part of or incorporated in Your product or as a standalone
311 Software maintenance update for existing end users of Your products,
312 excluding any other standalone products, subject to these conditions:
313
314 1. This Software is licensed for use only in conjunction with Intel
315 component products. Use of the Software in conjunction with non-Intel
316 component products is not licensed hereunder.
317 2. You may not copy, modify, rent, sell, distribute or transfer any part
318 of the Software except as provided in this Agreement, and you agree to
319 prevent unauthorized copying of the Software.
320 3. You may not reverse engineer, decompile, or disassemble the Software.
321 4. You may only distribute the Software to your customers pursuant to a
322 written license agreement. Such license agreement may be a "break-the-
323 seal" license agreement. At a minimum such license shall safeguard
324 Intel's ownership rights to the Software.
325 5. The Software may include portions offered on terms in addition to those
326 set out here, as set out in a license accompanying those portions.
327
328 NO OTHER RIGHTS. No rights or licenses are granted by Intel to You, expressly
329 or by implication, with respect to any proprietary information or patent,
330 copyright, mask work, trademark, trade secret, or other intellectual property
331 right owned or controlled by Intel, except as expressly provided in this
332 Agreement.
333
334 OWNERSHIP OF SOFTWARE AND COPYRIGHTS. Title to all copies of the Software
335 remains with Intel or its suppliers. The Software is copyrighted and
336 protected by the laws of the United States and other countries, and
337 international treaty provisions. You may not remove any copyright notices
338 from the Software. Intel may make changes to the Software, or to items
339 referenced therein, at any time without notice, but is not obligated to
340 support or update the Software. Except as otherwise expressly provided, Intel
341 grants no express or implied right under Intel patents, copyrights,
342 trademarks, or other intellectual property rights. You may transfer the
343 Software only if the recipient agrees to be fully bound by these terms and if
344 you retain no copies of the Software.
345
346 LIMITED MEDIA WARRANTY. If the Software has been delivered by Intel on
347 physical media, Intel warrants the media to be free from material physical
348 defects for a period of ninety days after delivery by Intel. If such a defect
349 is found, return the media to Intel for replacement or alternate delivery of
350 the Software as Intel may select.
351
352 EXCLUSION OF OTHER WARRANTIES. EXCEPT AS PROVIDED ABOVE, THE SOFTWARE IS
353 PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED WARRANTY OF ANY KIND
354 INCLUDING WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT, OR FITNESS FOR A
355 PARTICULAR PURPOSE. Intel does not warrant or assume responsibility for the
356 accuracy or completeness of any information, text, graphics, links or other
357 items contained within the Software.
358
359 LIMITATION OF LIABILITY. IN NO EVENT SHALL INTEL OR ITS SUPPLIERS BE LIABLE
360 FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, LOST PROFITS,
361 BUSINESS INTERRUPTION, OR LOST INFORMATION) ARISING OUT OF THE USE OF OR
362 INABILITY TO USE THE SOFTWARE, EVEN IF INTEL HAS BEEN ADVISED OF THE
363 POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS PROHIBIT EXCLUSION OR
364 LIMITATION OF LIABILITY FOR IMPLIED WARRANTIES OR CONSEQUENTIAL OR INCIDENTAL
365 DAMAGES, SO THE ABOVE LIMITATION MAY NOT APPLY TO YOU. YOU MAY ALSO HAVE
366 OTHER LEGAL RIGHTS THAT VARY FROM JURISDICTION TO JURISDICTION.
367
368 TERMINATION OF THIS AGREEMENT. Intel may terminate this Agreement at any time
369 if you violate its terms. Upon termination, you will immediately destroy the
370 Software or return all copies of the Software to Intel.
371
372 APPLICABLE LAWS. Claims arising under this Agreement shall be governed by the
373 laws of California, excluding its principles of conflict of laws and the
374 United Nations Convention on Contracts for the Sale of Goods. You may not
375 export the Software in violation of applicable export laws and regulations.
376 Intel is not obligated under any other agreements unless they are in writing
377 and signed by an authorized representative of Intel.
378
379 GOVERNMENT RESTRICTED RIGHTS. The Software is provided with "RESTRICTED
380 RIGHTS." Use, duplication, or disclosure by the Government is subject to
381 restrictions as set forth in FAR52.227-14 and DFAR252.227-7013 et seq. or its
382 successor. Use of the Software by the Government constitutes acknowledgment
383 of Intel's proprietary rights therein. Contractor or Manufacturer is Intel
384 2200 Mission College Blvd., Santa Clara, CA 95052.
385
386
387 [FILE:505:files/Makefile]
388 # $FreeBSD: head/sysutils/devcpu-data/files/Makefile 470620 2018-05-22 15:09:38Z sbruno $
389
390 INTEL_UCODE=    intel-ucode
391 OUTPUT_DIR=     mcodes
392
393 all:    ucode
394 ucode:  ucode-split
395         mkdir -p $(OUTPUT_DIR)
396         cd ${OUTPUT_DIR} && \
397         for file in \
398           ../${INTEL_UCODE}/[0-9,a-f][0-9,a-f]-[0-9,a-f][0-9,a-f]-[0-9,a-f][0-9,a-f]; do \
399                 ../ucode-split $$file; \
400         done
401
402 # Use the host cc to compile ucode-split in case of cross-compile
403 ucode-split: ucode-split.c
404         cc ucode-split.c -o $@
405
406 clean:
407         rm -rf $(OUTPUT_DIR) ucode-split
408
409
410 [FILE:2227:files/microcode_update.in]
411 #!/bin/sh
412 #
413 # $FreeBSD: head/sysutils/devcpu-data/files/microcode_update.in 459084 2018-01-15 17:05:37Z swills $
414 #
415
416 # PROVIDE:      microcode_update
417 # REQUIRE:      root mountcritlocal
418 # KEYWORD:      nojail
419 # BEFORE:       SERVERS
420
421 #
422 # Add the following line to /etc/rc.conf to enable flow-capture:
423 # microcode_update_enable (bool):       Set it to "YES" to update microcode on startup
424 #                                       Set to "NO" by default.
425 # microcode_update_datadir (str):       Directory, microcode updates stored in.
426 #                                       Default is "%%DATADIR%%"
427 # microcode_update_cpus (str):          A list of cpus to update on startup, or "ALL" for all.
428 #                                       Example: microcode_update_cpus_cpus="0 CPU0"
429 #                                       Set to "ALL" by default. 
430 # microcode_update_flags (str):         Flags for cpucontrol(8).
431
432 . /etc/rc.subr
433
434 name="microcode_update"
435 rcvar=microcode_update_enable
436 stop_cmd=":"
437 start_precmd="microcode_update_prepare"
438 start_cmd="microcode_update_start"
439 requires_modules="cpuctl"
440
441 CMT="/usr/sbin/cpucontrol"
442
443 microcode_update_prepare()
444 {
445         if ! kldstat -q -m cpuctl; then
446                 if ! kldload cpuctl > /dev/null 2>&1; then
447                         warn "Can't load cpuctl module."
448                         return 1
449                 fi
450         fi
451 }
452
453 microcode_update_start()
454 {
455         echo "Updating CPU Microcode..."
456         if [ "${microcode_cpus}" = "ALL" ]; then
457                 ncpu=`/sbin/sysctl -n hw.ncpu`
458                 cpus=`jot ${ncpu} 0`;
459         else
460                 cpus=${microcode_cpus}
461         fi
462         for i in ${cpus}; do
463                 ${CMT} -u ${microcode_update_flags} \
464                     -d "${microcode_update_datadir}" /dev/cpuctl${i} 2>&1 | \
465                     logger -p daemon.notice -t microcode_update || \
466                     (echo "Microcode Update Failed." && exit 1)
467         done
468         if [ "${microcode_cpus}" = "ALL" ]; then
469                 CPUCONTROL_UPDATED=$(cpucontrol -h 2>&1 | grep -q -- -e; echo $?)
470                 if [ ${CPUCONTROL_UPDATED} -ne 0 ]; then
471                         echo "Please update your system in order to update CPU microcode."
472                 else
473                         ${CMT} -e /dev/cpuctl0 >/dev/null 2>&1
474                         if [ $? -ne 0 ]; then
475                                 echo "Re-evalulation of CPU flags Failed."
476                                 exit 1
477                         fi
478                 fi
479         fi
480         echo "Done."
481 }
482
483 load_rc_config $name
484
485 # Set default values
486 : ${microcode_update_enable="NO"}
487 : ${microcode_update_datadir="%%DATADIR%%"}
488 : ${microcode_cpus="ALL"}
489 : ${microcode_update_flags=""}
490
491 run_rc_command "$1"
492
493
494 [FILE:5668:files/ucode-split.c]
495 /*-
496  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
497  *
498  * Copyright (C) 2018 The FreeBSD Foundation.
499  *
500  * Redistribution and use in source and binary forms, with or without
501  * modification, are permitted provided that the following conditions
502  * are met:
503  * 1. Redistributions of source code must retain the above copyright
504  *    notice, this list of conditions and the following disclaimer.
505  * 2. Redistributions in binary form must reproduce the above copyright
506  *    notice, this list of conditions and the following disclaimer in the
507  *    documentation and/or other materials provided with the distribution.
508  *
509  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
510  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
511  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
512  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
513  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
514  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
515  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
516  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
517  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
518  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
519  * SUCH DAMAGE.
520  *
521  * $FreeBSD: head/sysutils/devcpu-data/files/ucode-split.c 470255 2018-05-18 01:28:23Z sbruno $
522  */
523
524 #include <err.h>
525 #include <fcntl.h>
526 #include <stdbool.h>
527 #include <stdint.h>
528 #include <stdio.h>
529 #include <stdlib.h>
530 #include <string.h>
531 #include <unistd.h>
532
533 static const size_t bufsize = 65536;
534
535 /* SDM vol 3 9.11.1 Intel microcode header. */
536 struct microcode_update_header {
537         uint32_t header_version;
538         uint32_t update_revision;
539         uint32_t date;                  /* BCD mmddyyyy */
540         uint32_t processor_signature;
541         uint32_t checksum;              /* Over update data and header */
542         uint32_t loader_revision;
543         uint32_t processor_flags;
544         uint32_t data_size;
545         uint32_t total_size;
546         uint32_t reserved[3];
547 };
548
549 /*
550  * SDM vol 2A CPUID EAX = 01h Returns Model, Family, Stepping Information.
551  * Caller must free the returned string.
552  */
553
554 static char *
555 format_signature(uint32_t signature)
556 {
557         char *buf;
558         unsigned family, model, stepping;
559
560         family = (signature & 0xf00) >> 8;
561         model = (signature & 0xf0) >> 4;
562         stepping = signature & 0xf;
563         if (family == 0x06 || family == 0x0f)
564                 model += (signature & 0xf0000) >> 12;
565         if (family == 0x0f)
566                 family += (signature & 0xff00000) >> 20;
567         asprintf(&buf, "%02x-%02x-%02x", family, model, stepping);
568         if (buf == NULL)
569                 err(1, "asprintf");
570         return (buf);
571 }
572
573 static void
574 dump_header(const struct microcode_update_header *hdr)
575 {
576         char *sig_str;
577         int i;
578         bool platformid_printed;
579
580         sig_str = format_signature(hdr->processor_signature);
581         printf("header version\t0x%x\n", hdr->header_version);
582         printf("revision\t0x%x\n", hdr->update_revision);
583         printf("date\t\t0x%x\t%04x-%02x-%02x\n", hdr->date,
584             hdr->date & 0xffff, (hdr->date & 0xff000000) >> 24,
585             (hdr->date & 0xff0000) >> 16);
586         printf("signature\t0x%x\t\t%s\n", hdr->processor_signature, sig_str);
587         printf("checksum\t0x%x\n", hdr->checksum);
588         printf("loader revision\t0x%x\n", hdr->loader_revision);
589         printf("processor flags\t0x%x", hdr->processor_flags);
590         platformid_printed = false;
591         for (i = 0; i < 8; i++) {
592                 if (hdr->processor_flags & 1 << i) {
593                         printf("%s%d", platformid_printed ? ", " : "\t\t", i);
594                         platformid_printed = true;
595                 }
596         }
597         printf("\n");
598         printf("datasize\t0x%x\t\t0x%x\n", hdr->data_size,
599             hdr->data_size != 0 ? hdr->data_size : 2000);
600         printf("size\t\t0x%x\t\t0x%x\n", hdr->total_size,
601             hdr->total_size != 0 ? hdr->total_size : 2048);
602         free(sig_str);
603 }
604
605 static void
606 usage(void)
607 {
608
609         printf("ucode-split [-v] microcode_file\n");
610         exit(1);
611 }
612
613 int
614 main(int argc, char *argv[])
615 {
616         struct microcode_update_header hdr;
617         char *buf, *output_file, *sig_str;
618         size_t len, resid;
619         ssize_t rv;
620         int c, ifd, ofd;
621         bool vflag;
622
623         vflag = false;
624         while ((c = getopt(argc, argv, "v")) != -1) {
625                 switch (c) {
626                 case 'v':
627                         vflag = true;
628                         break;
629                 default:
630                         usage();
631                 }
632         }
633         argc -= optind;
634         argv += optind;
635
636         if (argc != 1)
637                 usage();
638
639         ifd = open(argv[0], O_RDONLY);
640         if (ifd < 0)
641                 err(1, "open");
642
643         buf = malloc(bufsize);
644         if (buf == NULL)
645                 err(1, "malloc");
646
647         for (;;) {
648                 /* Read header. */
649                 rv = read(ifd, &hdr, sizeof(hdr));
650                 if (rv < 0) {
651                         err(1, "read");
652                 } else if (rv == 0) {
653                         break;
654                 } else if (rv < (ssize_t)sizeof(hdr)) {
655                         errx(1, "invalid microcode header");
656                 }
657                 if (hdr.header_version != 1)
658                         errx(1, "invalid header version");
659
660                 if (vflag)
661                         dump_header(&hdr);
662
663                 sig_str = format_signature(hdr.processor_signature);
664                 asprintf(&output_file, "%s.%02x", sig_str,
665                     hdr.processor_flags & 0xff);
666                 free(sig_str);
667                 if (output_file == NULL)
668                         err(1, "asprintf");
669                 ofd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
670                 if (ofd < 0)
671                         err(1, "open");
672
673                 /* Write header. */
674                 rv = write(ofd, &hdr, sizeof(hdr));
675                 if (rv < (ssize_t)sizeof(hdr))
676                         err(1, "write");
677
678                 /* Copy data. */
679                 resid = (hdr.total_size != 0 ? hdr.total_size : 2048) -
680                     sizeof(hdr);
681                 if (resid > 1 << 24) /* Arbitrary chosen maximum size. */
682                         errx(1, "header total_size too large");
683                 while (resid > 0) {
684                         len = resid < bufsize ? resid : bufsize;
685                         rv = read(ifd, buf, len);
686                         if (rv < 0)
687                                 err(1, "read");
688                         else if (rv < (ssize_t)len)
689                                 errx(1, "truncated microcode data");
690                         if (write(ofd, buf, len) < (ssize_t)len)
691                                 err(1, "write");
692                         resid -= len;
693                 }
694                 if (vflag)
695                         printf("written to %s\n\n", output_file);
696                 close(ofd);
697                 free(output_file);
698         }
699 }
700