shadow.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright 2014 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs <bskeggs@redhat.com>
  23. */
  24. #include "priv.h"
  25. #include <core/option.h>
  26. #include <subdev/bios.h>
  27. #include <subdev/bios/image.h>
  28. struct shadow {
  29. u32 skip;
  30. const struct nvbios_source *func;
  31. void *data;
  32. u32 size;
  33. int score;
  34. };
  35. static bool
  36. shadow_fetch(struct nvkm_bios *bios, struct shadow *mthd, u32 upto)
  37. {
  38. const u32 limit = (upto + 3) & ~3;
  39. const u32 start = bios->size;
  40. void *data = mthd->data;
  41. if (nvbios_extend(bios, limit) > 0) {
  42. u32 read = mthd->func->read(data, start, limit - start, bios);
  43. bios->size = start + read;
  44. }
  45. return bios->size >= upto;
  46. }
  47. static int
  48. shadow_image(struct nvkm_bios *bios, int idx, u32 offset, struct shadow *mthd)
  49. {
  50. struct nvkm_subdev *subdev = &bios->subdev;
  51. struct nvbios_image image;
  52. int score = 1;
  53. if (mthd->func->no_pcir) {
  54. image.base = 0;
  55. image.type = 0;
  56. image.size = mthd->func->size(mthd->data);
  57. image.last = 1;
  58. } else {
  59. if (!shadow_fetch(bios, mthd, offset + 0x1000)) {
  60. nvkm_debug(subdev, "%08x: header fetch failed\n",
  61. offset);
  62. return 0;
  63. }
  64. if (!nvbios_image(bios, idx, &image)) {
  65. nvkm_debug(subdev, "image %d invalid\n", idx);
  66. return 0;
  67. }
  68. }
  69. nvkm_debug(subdev, "%08x: type %02x, %d bytes\n",
  70. image.base, image.type, image.size);
  71. if (!shadow_fetch(bios, mthd, image.size)) {
  72. nvkm_debug(subdev, "%08x: fetch failed\n", image.base);
  73. return 0;
  74. }
  75. switch (image.type) {
  76. case 0x00:
  77. if (!mthd->func->ignore_checksum &&
  78. nvbios_checksum(&bios->data[image.base], image.size)) {
  79. nvkm_debug(subdev, "%08x: checksum failed\n",
  80. image.base);
  81. if (!mthd->func->require_checksum) {
  82. if (mthd->func->rw)
  83. score += 1;
  84. score += 1;
  85. } else
  86. return 0;
  87. } else {
  88. score += 3;
  89. }
  90. break;
  91. default:
  92. score += 3;
  93. break;
  94. }
  95. if (!image.last)
  96. score += shadow_image(bios, idx + 1, offset + image.size, mthd);
  97. return score;
  98. }
  99. static int
  100. shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
  101. {
  102. const struct nvbios_source *func = mthd->func;
  103. struct nvkm_subdev *subdev = &bios->subdev;
  104. if (func->name) {
  105. nvkm_debug(subdev, "trying %s...\n", name ? name : func->name);
  106. if (func->init) {
  107. mthd->data = func->init(bios, name);
  108. if (IS_ERR(mthd->data)) {
  109. mthd->data = NULL;
  110. return 0;
  111. }
  112. }
  113. mthd->score = shadow_image(bios, 0, 0, mthd);
  114. if (func->fini)
  115. func->fini(mthd->data);
  116. nvkm_debug(subdev, "scored %d\n", mthd->score);
  117. mthd->data = bios->data;
  118. mthd->size = bios->size;
  119. bios->data = NULL;
  120. bios->size = 0;
  121. }
  122. return mthd->score;
  123. }
  124. static u32
  125. shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
  126. {
  127. const struct firmware *fw = data;
  128. if (offset + length <= fw->size) {
  129. memcpy(bios->data + offset, fw->data + offset, length);
  130. return length;
  131. }
  132. return 0;
  133. }
  134. static void *
  135. shadow_fw_init(struct nvkm_bios *bios, const char *name)
  136. {
  137. struct device *dev = bios->subdev.device->dev;
  138. const struct firmware *fw;
  139. int ret = request_firmware(&fw, name, dev);
  140. if (ret)
  141. return ERR_PTR(-ENOENT);
  142. return (void *)fw;
  143. }
  144. static const struct nvbios_source
  145. shadow_fw = {
  146. .name = "firmware",
  147. .init = shadow_fw_init,
  148. .fini = (void(*)(void *))release_firmware,
  149. .read = shadow_fw_read,
  150. .rw = false,
  151. };
  152. int
  153. nvbios_shadow(struct nvkm_bios *bios)
  154. {
  155. struct nvkm_subdev *subdev = &bios->subdev;
  156. struct nvkm_device *device = subdev->device;
  157. struct shadow mthds[] = {
  158. { 0, &nvbios_of },
  159. { 0, &nvbios_ramin },
  160. { 0, &nvbios_rom },
  161. { 0, &nvbios_acpi_fast },
  162. { 4, &nvbios_acpi_slow },
  163. { 1, &nvbios_pcirom },
  164. { 1, &nvbios_platform },
  165. {}
  166. }, *mthd, *best = NULL;
  167. const char *optarg;
  168. char *source;
  169. int optlen;
  170. /* handle user-specified bios source */
  171. optarg = nvkm_stropt(device->cfgopt, "NvBios", &optlen);
  172. source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
  173. if (source) {
  174. /* try to match one of the built-in methods */
  175. for (mthd = mthds; mthd->func; mthd++) {
  176. if (mthd->func->name &&
  177. !strcasecmp(source, mthd->func->name)) {
  178. best = mthd;
  179. if (shadow_method(bios, mthd, NULL))
  180. break;
  181. }
  182. }
  183. /* otherwise, attempt to load as firmware */
  184. if (!best && (best = mthd)) {
  185. mthd->func = &shadow_fw;
  186. shadow_method(bios, mthd, source);
  187. mthd->func = NULL;
  188. }
  189. if (!best->score) {
  190. nvkm_error(subdev, "%s invalid\n", source);
  191. kfree(source);
  192. source = NULL;
  193. }
  194. }
  195. /* scan all potential bios sources, looking for best image */
  196. if (!best || !best->score) {
  197. for (mthd = mthds, best = mthd; mthd->func; mthd++) {
  198. if (!mthd->skip || best->score < mthd->skip) {
  199. if (shadow_method(bios, mthd, NULL)) {
  200. if (mthd->score > best->score)
  201. best = mthd;
  202. }
  203. }
  204. }
  205. }
  206. /* cleanup the ones we didn't use */
  207. for (mthd = mthds; mthd->func; mthd++) {
  208. if (mthd != best)
  209. kfree(mthd->data);
  210. }
  211. if (!best->score) {
  212. nvkm_error(subdev, "unable to locate usable image\n");
  213. return -EINVAL;
  214. }
  215. nvkm_debug(subdev, "using image from %s\n", best->func ?
  216. best->func->name : source);
  217. bios->data = best->data;
  218. bios->size = best->size;
  219. kfree(source);
  220. return 0;
  221. }