intel_lib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 bool cpu_signatures_match(unsigned int s1, unsigned int p1,
  33. unsigned int s2, unsigned int p2)
  34. {
  35. if (s1 != s2)
  36. return false;
  37. /* Processor flags are either both 0 ... */
  38. if (!p1 && !p2)
  39. return true;
  40. /* ... or they intersect. */
  41. return p1 & p2;
  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. * Returns 1 if update has been found, 0 otherwise.
  121. */
  122. int find_matching_signature(void *mc, unsigned int csig, int cpf)
  123. {
  124. struct microcode_header_intel *mc_hdr = mc;
  125. struct extended_sigtable *ext_hdr;
  126. struct extended_signature *ext_sig;
  127. int i;
  128. if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
  129. return 1;
  130. /* Look for ext. headers: */
  131. if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
  132. return 0;
  133. ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
  134. ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
  135. for (i = 0; i < ext_hdr->count; i++) {
  136. if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
  137. return 1;
  138. ext_sig++;
  139. }
  140. return 0;
  141. }
  142. /*
  143. * Returns 1 if update has been found, 0 otherwise.
  144. */
  145. int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
  146. {
  147. struct microcode_header_intel *mc_hdr = mc;
  148. if (mc_hdr->rev <= new_rev)
  149. return 0;
  150. return find_matching_signature(mc, csig, cpf);
  151. }
  152. EXPORT_SYMBOL_GPL(has_newer_microcode);