intel_lib.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Intel CPU Microcode Update Driver for Linux
  3. *
  4. * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
  5. * H Peter Anvin" <hpa@zytor.com>
  6. *
  7. * This driver allows to upgrade microcode on Intel processors
  8. * belonging to IA-32 family - PentiumPro, Pentium II,
  9. * Pentium III, Xeon, Pentium 4, etc.
  10. *
  11. * Reference: Section 8.11 of Volume 3a, IA-32 Intel? Architecture
  12. * Software Developer's Manual
  13. * Order Number 253668 or free download from:
  14. *
  15. * http://developer.intel.com/Assets/PDF/manual/253668.pdf
  16. *
  17. * For more information, go to http://www.urbanmyth.org/microcode
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License
  21. * as published by the Free Software Foundation; either version
  22. * 2 of the License, or (at your option) any later version.
  23. *
  24. */
  25. #include <linux/firmware.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/kernel.h>
  28. #include <asm/microcode_intel.h>
  29. #include <asm/processor.h>
  30. #include <asm/msr.h>
  31. static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
  32. unsigned int s2, unsigned int p2)
  33. {
  34. if (s1 != s2)
  35. return false;
  36. /* Processor flags are either both 0 ... */
  37. if (!p1 && !p2)
  38. return true;
  39. /* ... or they intersect. */
  40. return p1 & p2;
  41. }
  42. int microcode_sanity_check(void *mc, int print_err)
  43. {
  44. unsigned long total_size, data_size, ext_table_size;
  45. struct microcode_header_intel *mc_header = mc;
  46. struct extended_sigtable *ext_header = NULL;
  47. u32 sum, orig_sum, ext_sigcount = 0, i;
  48. struct extended_signature *ext_sig;
  49. total_size = get_totalsize(mc_header);
  50. data_size = get_datasize(mc_header);
  51. if (data_size + MC_HEADER_SIZE > total_size) {
  52. if (print_err)
  53. pr_err("Error: bad microcode data file size.\n");
  54. return -EINVAL;
  55. }
  56. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  57. if (print_err)
  58. pr_err("Error: invalid/unknown microcode update format.\n");
  59. return -EINVAL;
  60. }
  61. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  62. if (ext_table_size) {
  63. u32 ext_table_sum = 0;
  64. u32 *ext_tablep;
  65. if ((ext_table_size < EXT_HEADER_SIZE)
  66. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  67. if (print_err)
  68. pr_err("Error: truncated extended signature table.\n");
  69. return -EINVAL;
  70. }
  71. ext_header = mc + MC_HEADER_SIZE + data_size;
  72. if (ext_table_size != exttable_size(ext_header)) {
  73. if (print_err)
  74. pr_err("Error: extended signature table size mismatch.\n");
  75. return -EFAULT;
  76. }
  77. ext_sigcount = ext_header->count;
  78. /*
  79. * Check extended table checksum: the sum of all dwords that
  80. * comprise a valid table must be 0.
  81. */
  82. ext_tablep = (u32 *)ext_header;
  83. i = ext_table_size / sizeof(u32);
  84. while (i--)
  85. ext_table_sum += ext_tablep[i];
  86. if (ext_table_sum) {
  87. if (print_err)
  88. pr_warn("Bad extended signature table checksum, aborting.\n");
  89. return -EINVAL;
  90. }
  91. }
  92. /*
  93. * Calculate the checksum of update data and header. The checksum of
  94. * valid update data and header including the extended signature table
  95. * must be 0.
  96. */
  97. orig_sum = 0;
  98. i = (MC_HEADER_SIZE + data_size) / sizeof(u32);
  99. while (i--)
  100. orig_sum += ((u32 *)mc)[i];
  101. if (orig_sum) {
  102. if (print_err)
  103. pr_err("Bad microcode data checksum, aborting.\n");
  104. return -EINVAL;
  105. }
  106. if (!ext_table_size)
  107. return 0;
  108. /*
  109. * Check extended signature checksum: 0 => valid.
  110. */
  111. for (i = 0; i < ext_sigcount; i++) {
  112. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  113. EXT_SIGNATURE_SIZE * i;
  114. sum = (mc_header->sig + mc_header->pf + mc_header->cksum) -
  115. (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  116. if (sum) {
  117. if (print_err)
  118. pr_err("Bad extended signature checksum, aborting.\n");
  119. return -EINVAL;
  120. }
  121. }
  122. return 0;
  123. }
  124. /*
  125. * Returns 1 if update has been found, 0 otherwise.
  126. */
  127. int find_matching_signature(void *mc, unsigned int csig, int cpf)
  128. {
  129. struct microcode_header_intel *mc_hdr = mc;
  130. struct extended_sigtable *ext_hdr;
  131. struct extended_signature *ext_sig;
  132. int i;
  133. if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
  134. return 1;
  135. /* Look for ext. headers: */
  136. if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
  137. return 0;
  138. ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
  139. ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
  140. for (i = 0; i < ext_hdr->count; i++) {
  141. if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
  142. return 1;
  143. ext_sig++;
  144. }
  145. return 0;
  146. }
  147. /*
  148. * Returns 1 if update has been found, 0 otherwise.
  149. */
  150. int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
  151. {
  152. struct microcode_header_intel *mc_hdr = mc;
  153. if (mc_hdr->rev <= new_rev)
  154. return 0;
  155. return find_matching_signature(mc, csig, cpf);
  156. }