shadow.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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->rw)
  82. score += 1;
  83. score += 1;
  84. } else {
  85. score += 3;
  86. }
  87. break;
  88. default:
  89. score += 3;
  90. break;
  91. }
  92. if (!image.last)
  93. score += shadow_image(bios, idx + 1, offset + image.size, mthd);
  94. return score;
  95. }
  96. static int
  97. shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name)
  98. {
  99. const struct nvbios_source *func = mthd->func;
  100. struct nvkm_subdev *subdev = &bios->subdev;
  101. if (func->name) {
  102. nvkm_debug(subdev, "trying %s...\n", name ? name : func->name);
  103. if (func->init) {
  104. mthd->data = func->init(bios, name);
  105. if (IS_ERR(mthd->data)) {
  106. mthd->data = NULL;
  107. return 0;
  108. }
  109. }
  110. mthd->score = shadow_image(bios, 0, 0, mthd);
  111. if (func->fini)
  112. func->fini(mthd->data);
  113. nvkm_debug(subdev, "scored %d\n", mthd->score);
  114. mthd->data = bios->data;
  115. mthd->size = bios->size;
  116. bios->data = NULL;
  117. bios->size = 0;
  118. }
  119. return mthd->score;
  120. }
  121. static u32
  122. shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
  123. {
  124. const struct firmware *fw = data;
  125. if (offset + length <= fw->size) {
  126. memcpy(bios->data + offset, fw->data + offset, length);
  127. return length;
  128. }
  129. return 0;
  130. }
  131. static void *
  132. shadow_fw_init(struct nvkm_bios *bios, const char *name)
  133. {
  134. struct device *dev = bios->subdev.device->dev;
  135. const struct firmware *fw;
  136. int ret = request_firmware(&fw, name, dev);
  137. if (ret)
  138. return ERR_PTR(-ENOENT);
  139. return (void *)fw;
  140. }
  141. static const struct nvbios_source
  142. shadow_fw = {
  143. .name = "firmware",
  144. .init = shadow_fw_init,
  145. .fini = (void(*)(void *))release_firmware,
  146. .read = shadow_fw_read,
  147. .rw = false,
  148. };
  149. int
  150. nvbios_shadow(struct nvkm_bios *bios)
  151. {
  152. struct nvkm_subdev *subdev = &bios->subdev;
  153. struct nvkm_device *device = subdev->device;
  154. struct shadow mthds[] = {
  155. { 0, &nvbios_of },
  156. { 0, &nvbios_ramin },
  157. { 0, &nvbios_rom },
  158. { 0, &nvbios_acpi_fast },
  159. { 4, &nvbios_acpi_slow },
  160. { 1, &nvbios_pcirom },
  161. { 1, &nvbios_platform },
  162. {}
  163. }, *mthd, *best = NULL;
  164. const char *optarg;
  165. char *source;
  166. int optlen;
  167. /* handle user-specified bios source */
  168. optarg = nvkm_stropt(device->cfgopt, "NvBios", &optlen);
  169. source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL;
  170. if (source) {
  171. /* try to match one of the built-in methods */
  172. for (mthd = mthds; mthd->func; mthd++) {
  173. if (mthd->func->name &&
  174. !strcasecmp(source, mthd->func->name)) {
  175. best = mthd;
  176. if (shadow_method(bios, mthd, NULL))
  177. break;
  178. }
  179. }
  180. /* otherwise, attempt to load as firmware */
  181. if (!best && (best = mthd)) {
  182. mthd->func = &shadow_fw;
  183. shadow_method(bios, mthd, source);
  184. mthd->func = NULL;
  185. }
  186. if (!best->score) {
  187. nvkm_error(subdev, "%s invalid\n", source);
  188. kfree(source);
  189. source = NULL;
  190. }
  191. }
  192. /* scan all potential bios sources, looking for best image */
  193. if (!best || !best->score) {
  194. for (mthd = mthds, best = mthd; mthd->func; mthd++) {
  195. if (!mthd->skip || best->score < mthd->skip) {
  196. if (shadow_method(bios, mthd, NULL)) {
  197. if (mthd->score > best->score)
  198. best = mthd;
  199. }
  200. }
  201. }
  202. }
  203. /* cleanup the ones we didn't use */
  204. for (mthd = mthds; mthd->func; mthd++) {
  205. if (mthd != best)
  206. kfree(mthd->data);
  207. }
  208. if (!best->score) {
  209. nvkm_error(subdev, "unable to locate usable image\n");
  210. return -EINVAL;
  211. }
  212. nvkm_debug(subdev, "using image from %s\n", best->func ?
  213. best->func->name : source);
  214. bios->data = best->data;
  215. bios->size = best->size;
  216. kfree(source);
  217. return 0;
  218. }