hs_ucode.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "hs_ucode.h"
  23. #include "ls_ucode.h"
  24. #include "acr.h"
  25. #include <engine/falcon.h>
  26. /**
  27. * hs_ucode_patch_signature() - patch HS blob with correct signature for
  28. * specified falcon.
  29. */
  30. static void
  31. hs_ucode_patch_signature(const struct nvkm_falcon *falcon, void *acr_image,
  32. bool new_format)
  33. {
  34. struct fw_bin_header *hsbin_hdr = acr_image;
  35. struct hsf_fw_header *fw_hdr = acr_image + hsbin_hdr->header_offset;
  36. void *hs_data = acr_image + hsbin_hdr->data_offset;
  37. void *sig;
  38. u32 sig_size;
  39. u32 patch_loc, patch_sig;
  40. /*
  41. * I had the brilliant idea to "improve" the binary format by
  42. * removing this useless indirection. However to make NVIDIA files
  43. * directly compatible, let's support both format.
  44. */
  45. if (new_format) {
  46. patch_loc = fw_hdr->patch_loc;
  47. patch_sig = fw_hdr->patch_sig;
  48. } else {
  49. patch_loc = *(u32 *)(acr_image + fw_hdr->patch_loc);
  50. patch_sig = *(u32 *)(acr_image + fw_hdr->patch_sig);
  51. }
  52. /* Falcon in debug or production mode? */
  53. if (falcon->debug) {
  54. sig = acr_image + fw_hdr->sig_dbg_offset;
  55. sig_size = fw_hdr->sig_dbg_size;
  56. } else {
  57. sig = acr_image + fw_hdr->sig_prod_offset;
  58. sig_size = fw_hdr->sig_prod_size;
  59. }
  60. /* Patch signature */
  61. memcpy(hs_data + patch_loc, sig + patch_sig, sig_size);
  62. }
  63. void *
  64. hs_ucode_load_blob(struct nvkm_subdev *subdev, const struct nvkm_falcon *falcon,
  65. const char *fw)
  66. {
  67. void *acr_image;
  68. bool new_format;
  69. acr_image = nvkm_acr_load_firmware(subdev, fw, 0);
  70. if (IS_ERR(acr_image))
  71. return acr_image;
  72. /* detect the format to define how signature should be patched */
  73. switch (((u32 *)acr_image)[0]) {
  74. case 0x3b1d14f0:
  75. new_format = true;
  76. break;
  77. case 0x000010de:
  78. new_format = false;
  79. break;
  80. default:
  81. nvkm_error(subdev, "unknown header for HS blob %s\n", fw);
  82. return ERR_PTR(-EINVAL);
  83. }
  84. hs_ucode_patch_signature(falcon, acr_image, new_format);
  85. return acr_image;
  86. }