]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/FlashMap/Microcode.c
3 Copyright (c) 2004 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
18 Utility for working with microcode patch files in the Intel
19 Platform Innovation Framework for EFI build environment.
24 #include <string.h> // for memset()
26 #include <stdlib.h> // for malloc()
28 #define UINT32 unsigned int
30 #include "EfiUtilityMsgs.h"
31 #include "Microcode.h"
33 #define MAX_LINE_LEN 256
36 // Structure definition for a microcode header
39 unsigned int HeaderVersion
;
43 unsigned int Checksum
;
44 unsigned int LoaderVersion
;
45 unsigned int PlatformId
;
46 unsigned int DataSize
; // if 0, then TotalSize = 2048, and TotalSize field is invalid
47 unsigned int TotalSize
; // number of bytes
48 unsigned int Reserved
[3];
49 } MICROCODE_IMAGE_HEADER
;
59 MicrocodeConstructor (
66 Constructor of module Microcode
88 Destructor of module Microcode
111 Read a 32-bit microcode data value from a text file and convert to raw binary form.
114 InFptr - file pointer to input text file
115 Data - pointer to where to return the data parsed
118 STATUS_SUCCESS - no errors or warnings, Data contains valid information
119 STATUS_ERROR - errors were encountered
123 char Line
[MAX_LINE_LEN
];
126 Line
[MAX_LINE_LEN
- 1] = 0;
128 if (fgets (Line
, MAX_LINE_LEN
, InFptr
) == NULL
) {
132 // If it was a binary file, then it may have overwritten our null terminator
134 if (Line
[MAX_LINE_LEN
- 1] != 0) {
139 // dd 000000001h ; comment
144 for (cptr
= Line
; *cptr
&& isspace(*cptr
); cptr
++) {
146 if ((tolower(cptr
[0]) == 'd') && (tolower(cptr
[1]) == 'd') && isspace (cptr
[2])) {
148 // Skip blanks and look for a hex digit
151 for (; *cptr
&& isspace(*cptr
); cptr
++) {
153 if (isxdigit (*cptr
)) {
154 if (sscanf (cptr
, "%X", Data
) != 1) {
158 return STATUS_SUCCESS
;
171 Parse a microcode text file, and write the binary results to an output file.
174 InFileName - input text file to parse
175 OutFileName - output file to write raw binary data from parsed input file
178 STATUS_SUCCESS - no errors or warnings
179 STATUS_ERROR - errors were encountered
186 MICROCODE_IMAGE_HEADER
*Header
;
190 unsigned int Checksum
;
193 unsigned int TotalSize
;
195 Status
= STATUS_ERROR
;
200 // Open the input text file
202 if ((InFptr
= fopen (InFileName
, "r")) == NULL
) {
203 Error (NULL
, 0, 0, InFileName
, "failed to open input microcode file for reading");
207 // Make two passes on the input file. The first pass is to determine how
208 // much data is in the file so we can allocate a working buffer. Then
209 // we'll allocate a buffer and re-read the file into the buffer for processing.
213 Status
= MicrocodeReadData (InFptr
, &Data
);
214 if (Status
== STATUS_SUCCESS
) {
215 Size
+= sizeof (Data
);
217 } while (Status
== STATUS_SUCCESS
);
222 Error (NULL
, 0, 0, InFileName
, "no parse-able data found in file");
225 if (Size
< sizeof (MICROCODE_IMAGE_HEADER
)) {
226 Error (NULL
, 0, 0, InFileName
, "amount of parse-able data is insufficient to contain a microcode header");
230 // Allocate a buffer for the data
232 Buffer
= (char *) _malloc (Size
);
233 if (Buffer
== NULL
) {
234 Error (NULL
, 0, 0, "memory allocation failure", NULL
);
238 // Re-read the file, storing the data into our buffer
240 fseek (InFptr
, 0, SEEK_SET
);
243 Status
= MicrocodeReadData (InFptr
, &Data
);
244 if (Status
== STATUS_SUCCESS
) {
245 *(unsigned int *) Ptr
= Data
;
246 Ptr
+= sizeof (Data
);
248 } while (Status
== STATUS_SUCCESS
);
250 // Can't do much checking on the header because, per the spec, the
251 // DataSize field may be 0, which means DataSize = 2000 and TotalSize = 2K,
252 // and the TotalSize field is invalid (actually missing). Thus we can't
253 // even verify the Reserved fields are 0.
255 Header
= (MICROCODE_IMAGE_HEADER
*) Buffer
;
256 if (Header
->DataSize
== 0) {
259 TotalSize
= Header
->TotalSize
;
261 if (TotalSize
!= Size
) {
262 Error (NULL
, 0, 0, InFileName
, "file contents do not contain expected TotalSize 0x%04X", TotalSize
);
266 // Checksum the contents
271 while (Size2
< Size
) {
272 Checksum
+= *(unsigned int *) Ptr
;
277 Error (NULL
, 0, 0, InFileName
, "checksum failed on file contents");
281 // Open the output file and write the buffer contents
283 if ((OutFptr
= fopen (OutFileName
, "wb")) == NULL
) {
284 Error (NULL
, 0, 0, OutFileName
, "failed to open output microcode file for writing");
287 if (fwrite (Buffer
, Size
, 1, OutFptr
) != 1) {
288 Error (NULL
, 0, 0, OutFileName
, "failed to write microcode data to output file");
291 Status
= STATUS_SUCCESS
;
293 if (Buffer
!= NULL
) {
296 if (InFptr
!= NULL
) {
299 if (OutFptr
!= NULL
) {
301 if (Status
== STATUS_ERROR
) {
302 remove (OutFileName
);