intel-microcode2ucode.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Convert Intel microcode.dat into individual ucode files
  3. * named: intel-ucode/$family-$model-$stepping
  4. *
  5. * The subdir intel-ucode/ is created in the current working
  6. * directory. We get multiple ucodes in the same file, so they
  7. * are appended to an existing file. Make sure the directory
  8. * is empty before every run of the converter.
  9. *
  10. * Kay Sievers <kay.sievers@vrfy.org>
  11. */
  12. #ifndef _GNU_SOURCE
  13. # define _GNU_SOURCE 1
  14. #endif
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <limits.h>
  21. #include <stdbool.h>
  22. #include <inttypes.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <sys/stat.h>
  26. struct microcode_header_intel {
  27. unsigned int hdrver;
  28. unsigned int rev;
  29. unsigned int date;
  30. unsigned int sig;
  31. unsigned int cksum;
  32. unsigned int ldrver;
  33. unsigned int pf;
  34. unsigned int datasize;
  35. unsigned int totalsize;
  36. unsigned int reserved[3];
  37. };
  38. union mcbuf {
  39. struct microcode_header_intel hdr;
  40. unsigned int i[0];
  41. char c[0];
  42. };
  43. int main(int argc, char *argv[])
  44. {
  45. char *filename = "/lib/firmware/microcode.dat";
  46. FILE *f;
  47. char line[LINE_MAX];
  48. char buf[4000000];
  49. union mcbuf *mc;
  50. size_t bufsize, count, start;
  51. int rc = EXIT_SUCCESS;
  52. if (argv[1] != NULL)
  53. filename = argv[1];
  54. count = 0;
  55. mc = (union mcbuf *) buf;
  56. f = fopen(filename, "re");
  57. if (f == NULL) {
  58. printf("open %s: %m\n", filename);
  59. rc = EXIT_FAILURE;
  60. goto out;
  61. }
  62. while (fgets(line, sizeof(line), f) != NULL) {
  63. if (sscanf(line, "%x, %x, %x, %x",
  64. &mc->i[count],
  65. &mc->i[count + 1],
  66. &mc->i[count + 2],
  67. &mc->i[count + 3]) != 4)
  68. continue;
  69. count += 4;
  70. }
  71. fclose(f);
  72. bufsize = count * sizeof(int);
  73. printf("%s: %lu(%luk) bytes, %zu integers\n",
  74. filename,
  75. bufsize,
  76. bufsize / 1024,
  77. count);
  78. if (bufsize < sizeof(struct microcode_header_intel))
  79. goto out;
  80. mkdir("intel-ucode", 0750);
  81. start = 0;
  82. for (;;) {
  83. size_t size;
  84. unsigned int family, model, stepping;
  85. unsigned int year, month, day;
  86. mc = (union mcbuf *) &buf[start];
  87. if (mc->hdr.totalsize)
  88. size = mc->hdr.totalsize;
  89. else
  90. size = 2000 + sizeof(struct microcode_header_intel);
  91. if (mc->hdr.ldrver != 1 || mc->hdr.hdrver != 1) {
  92. printf("unknown version/format:\n");
  93. rc = EXIT_FAILURE;
  94. break;
  95. }
  96. /*
  97. * 0- 3 stepping
  98. * 4- 7 model
  99. * 8-11 family
  100. * 12-13 type
  101. * 16-19 extended model
  102. * 20-27 extended family
  103. */
  104. family = (mc->hdr.sig >> 8) & 0xf;
  105. if (family == 0xf)
  106. family += (mc->hdr.sig >> 20) & 0xff;
  107. model = (mc->hdr.sig >> 4) & 0x0f;
  108. if (family == 0x06)
  109. model += ((mc->hdr.sig >> 16) & 0x0f) << 4;
  110. stepping = mc->hdr.sig & 0x0f;
  111. year = mc->hdr.date & 0xffff;
  112. month = mc->hdr.date >> 24;
  113. day = (mc->hdr.date >> 16) & 0xff;
  114. if (asprintf(&filename, "intel-ucode/%02x-%02x-%02x", family, model, stepping) < 0) {
  115. rc = EXIT_FAILURE;
  116. goto out;
  117. }
  118. printf("\n");
  119. printf("%s\n", filename);
  120. printf("signature: 0x%02x\n", mc->hdr.sig);
  121. printf("flags: 0x%02x\n", mc->hdr.pf);
  122. printf("revision: 0x%02x\n", mc->hdr.rev);
  123. printf("date: %04x-%02x-%02x\n", year, month, day);
  124. printf("size: %zu\n", size);
  125. f = fopen(filename, "ae");
  126. if (f == NULL) {
  127. printf("open %s: %m\n", filename);
  128. rc = EXIT_FAILURE;
  129. goto out;
  130. }
  131. if (fwrite(mc, size, 1, f) != 1) {
  132. printf("write %s: %m\n", filename);
  133. rc = EXIT_FAILURE;
  134. goto out;
  135. }
  136. fclose(f);
  137. free(filename);
  138. start += size;
  139. if (start >= bufsize)
  140. break;
  141. }
  142. printf("\n");
  143. out:
  144. return rc;
  145. }