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 26 #include <core/option.h> 27 #include <subdev/bios.h> 28 #include <subdev/bios/image.h> 29 30 struct shadow { 31 u32 skip; 32 const struct nvbios_source *func; 33 void *data; 34 u32 size; 35 int score; 36 }; 37 38 static bool 39 shadow_fetch(struct nvkm_bios *bios, struct shadow *mthd, u32 upto) 40 { 41 const u32 limit = (upto + 3) & ~3; 42 const u32 start = bios->size; 43 void *data = mthd->data; 44 if (nvbios_extend(bios, limit) > 0) { 45 u32 read = mthd->func->read(data, start, limit - start, bios); 46 bios->size = start + read; 47 } 48 return bios->size >= limit; 49 } 50 51 static int 52 shadow_image(struct nvkm_bios *bios, int idx, u32 offset, struct shadow *mthd) 53 { 54 struct nvkm_subdev *subdev = &bios->subdev; 55 struct nvbios_image image; 56 int score = 1; 57 58 if (!shadow_fetch(bios, mthd, offset + 0x1000)) { 59 nvkm_debug(subdev, "%08x: header fetch failed\n", offset); 60 return 0; 61 } 62 63 if (!nvbios_image(bios, idx, &image)) { 64 nvkm_debug(subdev, "image %d invalid\n", idx); 65 return 0; 66 } 67 nvkm_debug(subdev, "%08x: type %02x, %d bytes\n", 68 image.base, image.type, image.size); 69 70 if (!shadow_fetch(bios, mthd, image.size)) { 71 nvkm_debug(subdev, "%08x: fetch failed\n", image.base); 72 return 0; 73 } 74 75 switch (image.type) { 76 case 0x00: 77 if (nvbios_checksum(&bios->data[image.base], image.size)) { 78 nvkm_debug(subdev, "%08x: checksum failed\n", 79 image.base); 80 if (mthd->func->rw) 81 score += 1; 82 score += 1; 83 } else { 84 score += 3; 85 } 86 break; 87 default: 88 score += 3; 89 break; 90 } 91 92 if (!image.last) 93 score += shadow_image(bios, idx + 1, offset + image.size, mthd); 94 return score; 95 } 96 97 static int 98 shadow_method(struct nvkm_bios *bios, struct shadow *mthd, const char *name) 99 { 100 const struct nvbios_source *func = mthd->func; 101 struct nvkm_subdev *subdev = &bios->subdev; 102 if (func->name) { 103 nvkm_debug(subdev, "trying %s...\n", name ? name : func->name); 104 if (func->init) { 105 mthd->data = func->init(bios, name); 106 if (IS_ERR(mthd->data)) { 107 mthd->data = NULL; 108 return 0; 109 } 110 } 111 mthd->score = shadow_image(bios, 0, 0, mthd); 112 if (func->fini) 113 func->fini(mthd->data); 114 nvkm_debug(subdev, "scored %d\n", mthd->score); 115 mthd->data = bios->data; 116 mthd->size = bios->size; 117 bios->data = NULL; 118 bios->size = 0; 119 } 120 return mthd->score; 121 } 122 123 static u32 124 shadow_fw_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios) 125 { 126 const struct firmware *fw = data; 127 if (offset + length <= fw->size) { 128 memcpy(bios->data + offset, fw->data + offset, length); 129 return length; 130 } 131 return 0; 132 } 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 145 static const struct nvbios_source 146 shadow_fw = { 147 .name = "firmware", 148 .init = shadow_fw_init, 149 .fini = (void(*)(void *))release_firmware, 150 .read = shadow_fw_read, 151 .rw = false, 152 }; 153 154 int 155 nvbios_shadow(struct nvkm_bios *bios) 156 { 157 struct nvkm_subdev *subdev = &bios->subdev; 158 struct nvkm_device *device = subdev->device; 159 struct shadow mthds[] = { 160 { 0, &nvbios_of }, 161 { 0, &nvbios_ramin }, 162 { 0, &nvbios_rom }, 163 { 0, &nvbios_acpi_fast }, 164 { 4, &nvbios_acpi_slow }, 165 { 1, &nvbios_pcirom }, 166 { 1, &nvbios_platform }, 167 {} 168 }, *mthd, *best = NULL; 169 const char *optarg; 170 char *source; 171 int optlen; 172 173 /* handle user-specified bios source */ 174 optarg = nvkm_stropt(device->cfgopt, "NvBios", &optlen); 175 source = optarg ? kstrndup(optarg, optlen, GFP_KERNEL) : NULL; 176 if (source) { 177 /* try to match one of the built-in methods */ 178 for (mthd = mthds; mthd->func; mthd++) { 179 if (mthd->func->name && 180 !strcasecmp(source, mthd->func->name)) { 181 best = mthd; 182 if (shadow_method(bios, mthd, NULL)) 183 break; 184 } 185 } 186 187 /* otherwise, attempt to load as firmware */ 188 if (!best && (best = mthd)) { 189 mthd->func = &shadow_fw; 190 shadow_method(bios, mthd, source); 191 mthd->func = NULL; 192 } 193 194 if (!best->score) { 195 nvkm_error(subdev, "%s invalid\n", source); 196 kfree(source); 197 source = NULL; 198 } 199 } 200 201 /* scan all potential bios sources, looking for best image */ 202 if (!best || !best->score) { 203 for (mthd = mthds, best = mthd; mthd->func; mthd++) { 204 if (!mthd->skip || best->score < mthd->skip) { 205 if (shadow_method(bios, mthd, NULL)) { 206 if (mthd->score > best->score) 207 best = mthd; 208 } 209 } 210 } 211 } 212 213 /* cleanup the ones we didn't use */ 214 for (mthd = mthds; mthd->func; mthd++) { 215 if (mthd != best) 216 kfree(mthd->data); 217 } 218 219 if (!best->score) { 220 nvkm_error(subdev, "unable to locate usable image\n"); 221 return -EINVAL; 222 } 223 224 nvkm_debug(subdev, "using image from %s\n", best->func ? 225 best->func->name : source); 226 bios->data = best->data; 227 bios->size = best->size; 228 kfree(source); 229 return 0; 230 } 231