1 /*
2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "hs_ucode.h"
24 #include "ls_ucode.h"
25 #include "acr.h"
26
27 #include <engine/falcon.h>
28
29 /**
30 * hs_ucode_patch_signature() - patch HS blob with correct signature for
31 * specified falcon.
32 */
33 static void
hs_ucode_patch_signature(const struct nvkm_falcon * falcon,void * acr_image,bool new_format)34 hs_ucode_patch_signature(const struct nvkm_falcon *falcon, void *acr_image,
35 bool new_format)
36 {
37 struct fw_bin_header *hsbin_hdr = acr_image;
38 struct hsf_fw_header *fw_hdr = acr_image + hsbin_hdr->header_offset;
39 void *hs_data = acr_image + hsbin_hdr->data_offset;
40 void *sig;
41 u32 sig_size;
42 u32 patch_loc, patch_sig;
43
44 /*
45 * I had the brilliant idea to "improve" the binary format by
46 * removing this useless indirection. However to make NVIDIA files
47 * directly compatible, let's support both format.
48 */
49 if (new_format) {
50 patch_loc = fw_hdr->patch_loc;
51 patch_sig = fw_hdr->patch_sig;
52 } else {
53 patch_loc = *(u32 *)(acr_image + fw_hdr->patch_loc);
54 patch_sig = *(u32 *)(acr_image + fw_hdr->patch_sig);
55 }
56
57 /* Falcon in debug or production mode? */
58 if (falcon->debug) {
59 sig = acr_image + fw_hdr->sig_dbg_offset;
60 sig_size = fw_hdr->sig_dbg_size;
61 } else {
62 sig = acr_image + fw_hdr->sig_prod_offset;
63 sig_size = fw_hdr->sig_prod_size;
64 }
65
66 /* Patch signature */
67 memcpy(hs_data + patch_loc, sig + patch_sig, sig_size);
68 }
69
70 void *
hs_ucode_load_blob(struct nvkm_subdev * subdev,const struct nvkm_falcon * falcon,const char * fw)71 hs_ucode_load_blob(struct nvkm_subdev *subdev, const struct nvkm_falcon *falcon,
72 const char *fw)
73 {
74 void *acr_image;
75 bool new_format;
76
77 acr_image = nvkm_acr_load_firmware(subdev, fw, 0);
78 if (IS_ERR(acr_image))
79 return acr_image;
80
81 /* detect the format to define how signature should be patched */
82 switch (((u32 *)acr_image)[0]) {
83 case 0x3b1d14f0:
84 new_format = true;
85 break;
86 case 0x000010de:
87 new_format = false;
88 break;
89 default:
90 nvkm_error(subdev, "unknown header for HS blob %s\n", fw);
91 return ERR_PTR(-EINVAL);
92 }
93
94 hs_ucode_patch_signature(falcon, acr_image, new_format);
95
96 return acr_image;
97 }
98