1 /* 2 * Copyright 2019 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 #include "priv.h" 23 #include <core/falcon.h> 24 #include <core/firmware.h> 25 #include <nvfw/fw.h> 26 #include <nvfw/ls.h> 27 28 void 29 nvkm_acr_lsfw_del(struct nvkm_acr_lsfw *lsfw) 30 { 31 nvkm_blob_dtor(&lsfw->img); 32 kfree(lsfw->sigs); 33 nvkm_firmware_put(lsfw->sig); 34 list_del(&lsfw->head); 35 kfree(lsfw); 36 } 37 38 void 39 nvkm_acr_lsfw_del_all(struct nvkm_acr *acr) 40 { 41 struct nvkm_acr_lsfw *lsfw, *lsft; 42 list_for_each_entry_safe(lsfw, lsft, &acr->lsfw, head) { 43 nvkm_acr_lsfw_del(lsfw); 44 } 45 } 46 47 static struct nvkm_acr_lsfw * 48 nvkm_acr_lsfw_get(struct nvkm_acr *acr, enum nvkm_acr_lsf_id id) 49 { 50 struct nvkm_acr_lsfw *lsfw; 51 list_for_each_entry(lsfw, &acr->lsfw, head) { 52 if (lsfw->id == id) 53 return lsfw; 54 } 55 return NULL; 56 } 57 58 struct nvkm_acr_lsfw * 59 nvkm_acr_lsfw_add(const struct nvkm_acr_lsf_func *func, struct nvkm_acr *acr, 60 struct nvkm_falcon *falcon, enum nvkm_acr_lsf_id id) 61 { 62 struct nvkm_acr_lsfw *lsfw; 63 64 if (!acr || list_empty(&acr->hsfw)) 65 return ERR_PTR(-ENOSYS); 66 67 lsfw = nvkm_acr_lsfw_get(acr, id); 68 if (lsfw && lsfw->func) { 69 nvkm_error(&acr->subdev, "LSFW %d redefined\n", id); 70 return ERR_PTR(-EEXIST); 71 } 72 73 if (!lsfw) { 74 if (!(lsfw = kzalloc(sizeof(*lsfw), GFP_KERNEL))) 75 return ERR_PTR(-ENOMEM); 76 77 lsfw->id = id; 78 list_add_tail(&lsfw->head, &acr->lsfw); 79 } 80 81 lsfw->func = func; 82 lsfw->falcon = falcon; 83 return lsfw; 84 } 85 86 static struct nvkm_acr_lsfw * 87 nvkm_acr_lsfw_load_sig_image_desc_(struct nvkm_subdev *subdev, 88 struct nvkm_falcon *falcon, 89 enum nvkm_acr_lsf_id id, 90 const char *path, int ver, 91 const struct nvkm_acr_lsf_func *func, 92 const struct firmware **pdesc) 93 { 94 struct nvkm_acr *acr = subdev->device->acr; 95 struct nvkm_acr_lsfw *lsfw; 96 int ret; 97 98 if (IS_ERR((lsfw = nvkm_acr_lsfw_add(func, acr, falcon, id)))) 99 return lsfw; 100 101 ret = nvkm_firmware_load_name(subdev, path, "sig", ver, &lsfw->sig); 102 if (ret) 103 goto done; 104 105 ret = nvkm_firmware_load_blob(subdev, path, "image", ver, &lsfw->img); 106 if (ret) 107 goto done; 108 109 ret = nvkm_firmware_load_name(subdev, path, "desc", ver, pdesc); 110 done: 111 if (ret) { 112 nvkm_acr_lsfw_del(lsfw); 113 return ERR_PTR(ret); 114 } 115 116 return lsfw; 117 } 118 119 static void 120 nvkm_acr_lsfw_from_desc(const struct nvfw_ls_desc_head *desc, 121 struct nvkm_acr_lsfw *lsfw) 122 { 123 lsfw->bootloader_size = ALIGN(desc->bootloader_size, 256); 124 lsfw->bootloader_imem_offset = desc->bootloader_imem_offset; 125 126 lsfw->app_size = ALIGN(desc->app_size, 256); 127 lsfw->app_start_offset = desc->app_start_offset; 128 lsfw->app_imem_entry = desc->app_imem_entry; 129 lsfw->app_resident_code_offset = desc->app_resident_code_offset; 130 lsfw->app_resident_code_size = desc->app_resident_code_size; 131 lsfw->app_resident_data_offset = desc->app_resident_data_offset; 132 lsfw->app_resident_data_size = desc->app_resident_data_size; 133 134 lsfw->ucode_size = ALIGN(lsfw->app_resident_data_offset, 256) + 135 lsfw->bootloader_size; 136 lsfw->data_size = lsfw->app_size + lsfw->bootloader_size - 137 lsfw->ucode_size; 138 } 139 140 int 141 nvkm_acr_lsfw_load_sig_image_desc(struct nvkm_subdev *subdev, 142 struct nvkm_falcon *falcon, 143 enum nvkm_acr_lsf_id id, 144 const char *path, int ver, 145 const struct nvkm_acr_lsf_func *func) 146 { 147 const struct firmware *fw; 148 struct nvkm_acr_lsfw *lsfw; 149 150 lsfw = nvkm_acr_lsfw_load_sig_image_desc_(subdev, falcon, id, path, ver, 151 func, &fw); 152 if (IS_ERR(lsfw)) 153 return PTR_ERR(lsfw); 154 155 nvkm_acr_lsfw_from_desc(&nvfw_ls_desc(subdev, fw->data)->head, lsfw); 156 nvkm_firmware_put(fw); 157 return 0; 158 } 159 160 int 161 nvkm_acr_lsfw_load_sig_image_desc_v1(struct nvkm_subdev *subdev, 162 struct nvkm_falcon *falcon, 163 enum nvkm_acr_lsf_id id, 164 const char *path, int ver, 165 const struct nvkm_acr_lsf_func *func) 166 { 167 const struct firmware *fw; 168 struct nvkm_acr_lsfw *lsfw; 169 170 lsfw = nvkm_acr_lsfw_load_sig_image_desc_(subdev, falcon, id, path, ver, 171 func, &fw); 172 if (IS_ERR(lsfw)) 173 return PTR_ERR(lsfw); 174 175 nvkm_acr_lsfw_from_desc(&nvfw_ls_desc_v1(subdev, fw->data)->head, lsfw); 176 nvkm_firmware_put(fw); 177 return 0; 178 } 179 180 int 181 nvkm_acr_lsfw_load_sig_image_desc_v2(struct nvkm_subdev *subdev, 182 struct nvkm_falcon *falcon, 183 enum nvkm_acr_lsf_id id, 184 const char *path, int ver, 185 const struct nvkm_acr_lsf_func *func) 186 { 187 const struct firmware *fw; 188 struct nvkm_acr_lsfw *lsfw; 189 const struct nvfw_ls_desc_v2 *desc; 190 int ret = 0; 191 192 lsfw = nvkm_acr_lsfw_load_sig_image_desc_(subdev, falcon, id, path, ver, func, &fw); 193 if (IS_ERR(lsfw)) 194 return PTR_ERR(lsfw); 195 196 desc = nvfw_ls_desc_v2(subdev, fw->data); 197 198 lsfw->secure_bootloader = desc->secure_bootloader; 199 lsfw->bootloader_size = ALIGN(desc->bootloader_size, 256); 200 lsfw->bootloader_imem_offset = desc->bootloader_imem_offset; 201 202 lsfw->app_size = ALIGN(desc->app_size, 256); 203 lsfw->app_start_offset = desc->app_start_offset; 204 lsfw->app_imem_entry = desc->app_imem_entry; 205 lsfw->app_resident_code_offset = desc->app_resident_code_offset; 206 lsfw->app_resident_code_size = desc->app_resident_code_size; 207 lsfw->app_resident_data_offset = desc->app_resident_data_offset; 208 lsfw->app_resident_data_size = desc->app_resident_data_size; 209 lsfw->app_imem_offset = desc->app_imem_offset; 210 lsfw->app_dmem_offset = desc->app_dmem_offset; 211 212 lsfw->ucode_size = ALIGN(lsfw->app_resident_data_offset, 256) + lsfw->bootloader_size; 213 lsfw->data_size = lsfw->app_size + lsfw->bootloader_size - lsfw->ucode_size; 214 215 nvkm_firmware_put(fw); 216 217 if (lsfw->secure_bootloader) { 218 const struct firmware *hsbl; 219 const struct nvfw_ls_hsbl_bin_hdr *hdr; 220 const struct nvfw_ls_hsbl_hdr *hshdr; 221 u32 sig, cnt, *meta; 222 223 ret = nvkm_firmware_load_name(subdev, path, "hs_bl_sig", ver, &hsbl); 224 if (ret) 225 return ret; 226 227 hdr = nvfw_ls_hsbl_bin_hdr(subdev, hsbl->data); 228 hshdr = nvfw_ls_hsbl_hdr(subdev, hsbl->data + hdr->header_offset); 229 meta = (u32 *)(hsbl->data + hshdr->meta_data_offset); 230 sig = *(u32 *)(hsbl->data + hshdr->patch_sig); 231 cnt = *(u32 *)(hsbl->data + hshdr->num_sig); 232 233 lsfw->fuse_ver = meta[0]; 234 lsfw->engine_id = meta[1]; 235 lsfw->ucode_id = meta[2]; 236 lsfw->sig_size = hshdr->sig_prod_size / cnt; 237 lsfw->sig_nr = cnt; 238 lsfw->sigs = kmemdup(hsbl->data + hshdr->sig_prod_offset + sig, 239 lsfw->sig_nr * lsfw->sig_size, GFP_KERNEL); 240 nvkm_firmware_put(hsbl); 241 if (!lsfw->sigs) 242 ret = -ENOMEM; 243 } 244 245 return ret; 246 } 247 248 int 249 nvkm_acr_lsfw_load_bl_inst_data_sig(struct nvkm_subdev *subdev, 250 struct nvkm_falcon *falcon, 251 enum nvkm_acr_lsf_id id, 252 const char *path, int ver, 253 const struct nvkm_acr_lsf_func *func) 254 { 255 struct nvkm_acr *acr = subdev->device->acr; 256 struct nvkm_acr_lsfw *lsfw; 257 const struct firmware *bl = NULL, *inst = NULL, *data = NULL; 258 const struct nvfw_bin_hdr *hdr; 259 const struct nvfw_bl_desc *desc; 260 u32 *bldata; 261 int ret; 262 263 if (IS_ERR((lsfw = nvkm_acr_lsfw_add(func, acr, falcon, id)))) 264 return PTR_ERR(lsfw); 265 266 ret = nvkm_firmware_load_name(subdev, path, "bl", ver, &bl); 267 if (ret) 268 goto done; 269 270 hdr = nvfw_bin_hdr(subdev, bl->data); 271 desc = nvfw_bl_desc(subdev, bl->data + hdr->header_offset); 272 bldata = (void *)(bl->data + hdr->data_offset); 273 274 ret = nvkm_firmware_load_name(subdev, path, "inst", ver, &inst); 275 if (ret) 276 goto done; 277 278 ret = nvkm_firmware_load_name(subdev, path, "data", ver, &data); 279 if (ret) 280 goto done; 281 282 ret = nvkm_firmware_load_name(subdev, path, "sig", ver, &lsfw->sig); 283 if (ret) 284 goto done; 285 286 lsfw->bootloader_size = ALIGN(desc->code_size, 256); 287 lsfw->bootloader_imem_offset = desc->start_tag << 8; 288 289 lsfw->app_start_offset = lsfw->bootloader_size; 290 lsfw->app_imem_entry = 0; 291 lsfw->app_resident_code_offset = 0; 292 lsfw->app_resident_code_size = ALIGN(inst->size, 256); 293 lsfw->app_resident_data_offset = lsfw->app_resident_code_size; 294 lsfw->app_resident_data_size = ALIGN(data->size, 256); 295 lsfw->app_size = lsfw->app_resident_code_size + 296 lsfw->app_resident_data_size; 297 298 lsfw->img.size = lsfw->bootloader_size + lsfw->app_size; 299 if (!(lsfw->img.data = kzalloc(lsfw->img.size, GFP_KERNEL))) { 300 ret = -ENOMEM; 301 goto done; 302 } 303 304 memcpy(lsfw->img.data, bldata, lsfw->bootloader_size); 305 memcpy(lsfw->img.data + lsfw->app_start_offset + 306 lsfw->app_resident_code_offset, inst->data, inst->size); 307 memcpy(lsfw->img.data + lsfw->app_start_offset + 308 lsfw->app_resident_data_offset, data->data, data->size); 309 310 lsfw->ucode_size = ALIGN(lsfw->app_resident_data_offset, 256) + 311 lsfw->bootloader_size; 312 lsfw->data_size = lsfw->app_size + lsfw->bootloader_size - 313 lsfw->ucode_size; 314 315 done: 316 if (ret) 317 nvkm_acr_lsfw_del(lsfw); 318 nvkm_firmware_put(data); 319 nvkm_firmware_put(inst); 320 nvkm_firmware_put(bl); 321 return ret; 322 } 323 324 int 325 nvkm_acr_lsfw_load_bl_sig_net(struct nvkm_subdev *subdev, 326 struct nvkm_falcon *falcon, 327 enum nvkm_acr_lsf_id id, 328 const char *path, int ver, 329 const struct nvkm_acr_lsf_func *func, 330 const void *inst_data, u32 inst_size, 331 const void *data_data, u32 data_size) 332 { 333 struct nvkm_acr *acr = subdev->device->acr; 334 struct nvkm_acr_lsfw *lsfw; 335 const struct firmware _inst = { .data = inst_data, .size = inst_size }; 336 const struct firmware _data = { .data = data_data, .size = data_size }; 337 const struct firmware *bl = NULL, *inst = &_inst, *data = &_data; 338 const struct { 339 int bin_magic; 340 int bin_version; 341 int bin_size; 342 int header_offset; 343 int header_size; 344 } *hdr; 345 u32 *bldata; 346 int ret; 347 348 if (IS_ERR((lsfw = nvkm_acr_lsfw_add(func, acr, falcon, id)))) 349 return PTR_ERR(lsfw); 350 351 ret = nvkm_firmware_load_name(subdev, path, "bl", ver, &bl); 352 if (ret) 353 goto done; 354 355 hdr = (const void *)bl->data; 356 bldata = (void *)(bl->data + hdr->header_offset); 357 358 ret = nvkm_firmware_load_name(subdev, path, "sig", ver, &lsfw->sig); 359 if (ret) 360 goto done; 361 362 lsfw->bootloader_size = ALIGN(hdr->header_size, 256); 363 lsfw->bootloader_imem_offset = func->bl_entry; 364 365 lsfw->app_start_offset = lsfw->bootloader_size; 366 lsfw->app_imem_entry = 0; 367 lsfw->app_resident_code_offset = 0; 368 lsfw->app_resident_code_size = ALIGN(inst->size, 256); 369 lsfw->app_resident_data_offset = lsfw->app_resident_code_size; 370 lsfw->app_resident_data_size = ALIGN(data->size, 256); 371 lsfw->app_imem_offset = 0; 372 lsfw->app_dmem_offset = 0; 373 lsfw->app_size = lsfw->app_resident_code_size + lsfw->app_resident_data_size; 374 375 lsfw->img.size = lsfw->bootloader_size + lsfw->app_size; 376 if (!(lsfw->img.data = kzalloc(lsfw->img.size, GFP_KERNEL))) { 377 ret = -ENOMEM; 378 goto done; 379 } 380 381 memcpy(lsfw->img.data, bldata, lsfw->bootloader_size); 382 memcpy(lsfw->img.data + lsfw->app_start_offset + 383 lsfw->app_resident_code_offset, inst->data, inst->size); 384 memcpy(lsfw->img.data + lsfw->app_start_offset + 385 lsfw->app_resident_data_offset, data->data, data->size); 386 387 lsfw->ucode_size = ALIGN(lsfw->app_resident_data_offset, 256) + 388 lsfw->bootloader_size; 389 lsfw->data_size = lsfw->app_size + lsfw->bootloader_size - 390 lsfw->ucode_size; 391 392 done: 393 if (ret) 394 nvkm_acr_lsfw_del(lsfw); 395 nvkm_firmware_put(bl); 396 return ret; 397 } 398