intel_lib.c 4.5 KB

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