microcode_intel_lib.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <linux/module.h>
  29. #include <asm/microcode_intel.h>
  30. #include <asm/processor.h>
  31. #include <asm/msr.h>
  32. static inline int
  33. update_match_cpu(unsigned int csig, unsigned int cpf,
  34. unsigned int sig, unsigned int pf)
  35. {
  36. return (!sigmatch(sig, csig, pf, cpf)) ? 0 : 1;
  37. }
  38. int
  39. update_match_revision(struct microcode_header_intel *mc_header, int rev)
  40. {
  41. return (mc_header->rev <= rev) ? 0 : 1;
  42. }
  43. int microcode_sanity_check(void *mc, int print_err)
  44. {
  45. unsigned long total_size, data_size, ext_table_size;
  46. struct microcode_header_intel *mc_header = mc;
  47. struct extended_sigtable *ext_header = NULL;
  48. int sum, orig_sum, ext_sigcount = 0, i;
  49. struct extended_signature *ext_sig;
  50. total_size = get_totalsize(mc_header);
  51. data_size = get_datasize(mc_header);
  52. if (data_size + MC_HEADER_SIZE > total_size) {
  53. if (print_err)
  54. pr_err("error! Bad data size in microcode data file\n");
  55. return -EINVAL;
  56. }
  57. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  58. if (print_err)
  59. pr_err("error! Unknown microcode update format\n");
  60. return -EINVAL;
  61. }
  62. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  63. if (ext_table_size) {
  64. if ((ext_table_size < EXT_HEADER_SIZE)
  65. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  66. if (print_err)
  67. pr_err("error! Small exttable size in microcode data file\n");
  68. return -EINVAL;
  69. }
  70. ext_header = mc + MC_HEADER_SIZE + data_size;
  71. if (ext_table_size != exttable_size(ext_header)) {
  72. if (print_err)
  73. pr_err("error! Bad exttable size in microcode data file\n");
  74. return -EFAULT;
  75. }
  76. ext_sigcount = ext_header->count;
  77. }
  78. /* check extended table checksum */
  79. if (ext_table_size) {
  80. int ext_table_sum = 0;
  81. int *ext_tablep = (int *)ext_header;
  82. i = ext_table_size / DWSIZE;
  83. while (i--)
  84. ext_table_sum += ext_tablep[i];
  85. if (ext_table_sum) {
  86. if (print_err)
  87. pr_warn("aborting, bad extended signature table checksum\n");
  88. return -EINVAL;
  89. }
  90. }
  91. /* calculate the checksum */
  92. orig_sum = 0;
  93. i = (MC_HEADER_SIZE + data_size) / DWSIZE;
  94. while (i--)
  95. orig_sum += ((int *)mc)[i];
  96. if (orig_sum) {
  97. if (print_err)
  98. pr_err("aborting, bad checksum\n");
  99. return -EINVAL;
  100. }
  101. if (!ext_table_size)
  102. return 0;
  103. /* check extended signature checksum */
  104. for (i = 0; i < ext_sigcount; i++) {
  105. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  106. EXT_SIGNATURE_SIZE * i;
  107. sum = orig_sum
  108. - (mc_header->sig + mc_header->pf + mc_header->cksum)
  109. + (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  110. if (sum) {
  111. if (print_err)
  112. pr_err("aborting, bad checksum\n");
  113. return -EINVAL;
  114. }
  115. }
  116. return 0;
  117. }
  118. EXPORT_SYMBOL_GPL(microcode_sanity_check);
  119. /*
  120. * return 0 - no update found
  121. * return 1 - found update
  122. */
  123. int get_matching_sig(unsigned int csig, int cpf, void *mc, int rev)
  124. {
  125. struct microcode_header_intel *mc_header = mc;
  126. struct extended_sigtable *ext_header;
  127. unsigned long total_size = get_totalsize(mc_header);
  128. int ext_sigcount, i;
  129. struct extended_signature *ext_sig;
  130. if (update_match_cpu(csig, cpf, mc_header->sig, mc_header->pf))
  131. return 1;
  132. /* Look for ext. headers: */
  133. if (total_size <= get_datasize(mc_header) + MC_HEADER_SIZE)
  134. return 0;
  135. ext_header = mc + get_datasize(mc_header) + MC_HEADER_SIZE;
  136. ext_sigcount = ext_header->count;
  137. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  138. for (i = 0; i < ext_sigcount; i++) {
  139. if (update_match_cpu(csig, cpf, ext_sig->sig, ext_sig->pf))
  140. return 1;
  141. ext_sig++;
  142. }
  143. return 0;
  144. }
  145. /*
  146. * return 0 - no update found
  147. * return 1 - found update
  148. */
  149. int get_matching_microcode(unsigned int csig, int cpf, void *mc, int rev)
  150. {
  151. struct microcode_header_intel *mc_header = mc;
  152. if (!update_match_revision(mc_header, rev))
  153. return 0;
  154. return get_matching_sig(csig, cpf, mc, rev);
  155. }
  156. EXPORT_SYMBOL_GPL(get_matching_microcode);