intel_lib.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. int 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 data size in microcode data file\n");
  54. return -EINVAL;
  55. }
  56. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  57. if (print_err)
  58. pr_err("error! 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. if ((ext_table_size < EXT_HEADER_SIZE)
  64. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  65. if (print_err)
  66. pr_err("error! Small exttable size in microcode data file\n");
  67. return -EINVAL;
  68. }
  69. ext_header = mc + MC_HEADER_SIZE + data_size;
  70. if (ext_table_size != exttable_size(ext_header)) {
  71. if (print_err)
  72. pr_err("error! Bad exttable size in microcode data file\n");
  73. return -EFAULT;
  74. }
  75. ext_sigcount = ext_header->count;
  76. }
  77. /* check extended table checksum */
  78. if (ext_table_size) {
  79. int ext_table_sum = 0;
  80. int *ext_tablep = (int *)ext_header;
  81. i = ext_table_size / DWSIZE;
  82. while (i--)
  83. ext_table_sum += ext_tablep[i];
  84. if (ext_table_sum) {
  85. if (print_err)
  86. pr_warn("aborting, bad extended signature table checksum\n");
  87. return -EINVAL;
  88. }
  89. }
  90. /* calculate the checksum */
  91. orig_sum = 0;
  92. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  93. while (i--)
  94. orig_sum += ((int *)mc)[i];
  95. if (orig_sum) {
  96. if (print_err)
  97. pr_err("aborting, bad checksum\n");
  98. return -EINVAL;
  99. }
  100. if (!ext_table_size)
  101. return 0;
  102. /* check extended signature checksum */
  103. for (i = 0; i < ext_sigcount; i++) {
  104. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  105. EXT_SIGNATURE_SIZE * i;
  106. sum = orig_sum
  107. - (mc_header->sig + mc_header->pf + mc_header->cksum)
  108. + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  109. if (sum) {
  110. if (print_err)
  111. pr_err("aborting, bad checksum\n");
  112. return -EINVAL;
  113. }
  114. }
  115. return 0;
  116. }
  117. EXPORT_SYMBOL_GPL(microcode_sanity_check);
  118. /*
  119. * Returns 1 if update has been found, 0 otherwise.
  120. */
  121. int find_matching_signature(void *mc, unsigned int csig, int cpf)
  122. {
  123. struct microcode_header_intel *mc_hdr = mc;
  124. struct extended_sigtable *ext_hdr;
  125. struct extended_signature *ext_sig;
  126. int i;
  127. if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
  128. return 1;
  129. /* Look for ext. headers: */
  130. if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
  131. return 0;
  132. ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
  133. ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
  134. for (i = 0; i < ext_hdr->count; i++) {
  135. if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
  136. return 1;
  137. ext_sig++;
  138. }
  139. return 0;
  140. }
  141. /*
  142. * Returns 1 if update has been found, 0 otherwise.
  143. */
  144. int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
  145. {
  146. struct microcode_header_intel *mc_hdr = mc;
  147. if (mc_hdr->rev <= new_rev)
  148. return 0;
  149. return find_matching_signature(mc, csig, cpf);
  150. }
  151. EXPORT_SYMBOL_GPL(has_newer_microcode);