blob.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * Command for encapsulating/decapsulating blob of memory.
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <malloc.h>
  9. #include <asm/byteorder.h>
  10. #include <linux/compiler.h>
  11. #if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7) || \
  12. defined(CONFIG_ARCH_MX7ULP) || defined(CONFIG_ARCH_IMX8M)
  13. #include <fsl_sec.h>
  14. #include <asm/arch/clock.h>
  15. #endif
  16. /**
  17. * blob_decap() - Decapsulate the data as a blob
  18. * @key_mod: - Pointer to key modifier/key
  19. * @src: - Address of data to be decapsulated
  20. * @dst: - Address of data to be decapsulated
  21. * @len: - Size of data to be decapsulated
  22. *
  23. * Returns zero on success,and negative on error.
  24. */
  25. __weak int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
  26. {
  27. return 0;
  28. }
  29. /**
  30. * blob_encap() - Encapsulate the data as a blob
  31. * @key_mod: - Pointer to key modifier/key
  32. * @src: - Address of data to be encapsulated
  33. * @dst: - Address of data to be encapsulated
  34. * @len: - Size of data to be encapsulated
  35. *
  36. * Returns zero on success,and negative on error.
  37. */
  38. __weak int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
  39. {
  40. return 0;
  41. }
  42. /**
  43. * do_blob() - Handle the "blob" command-line command
  44. * @cmdtp: Command data struct pointer
  45. * @flag: Command flag
  46. * @argc: Command-line argument count
  47. * @argv: Array of command-line arguments
  48. *
  49. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  50. * on error.
  51. */
  52. static int do_blob(struct cmd_tbl *cmdtp, int flag, int argc,
  53. char *const argv[])
  54. {
  55. ulong key_addr, src_addr, dst_addr, len;
  56. uint8_t *km_ptr, *src_ptr, *dst_ptr;
  57. int enc, ret = 0;
  58. if (argc != 6)
  59. return CMD_RET_USAGE;
  60. if (!strncmp(argv[1], "enc", 3))
  61. enc = 1;
  62. else if (!strncmp(argv[1], "dec", 3))
  63. enc = 0;
  64. else
  65. return CMD_RET_USAGE;
  66. src_addr = hextoul(argv[2], NULL);
  67. dst_addr = hextoul(argv[3], NULL);
  68. len = hextoul(argv[4], NULL);
  69. key_addr = hextoul(argv[5], NULL);
  70. km_ptr = (uint8_t *)(uintptr_t)key_addr;
  71. src_ptr = (uint8_t *)(uintptr_t)src_addr;
  72. dst_ptr = (uint8_t *)(uintptr_t)dst_addr;
  73. #if defined(CONFIG_ARCH_MX6) || defined(CONFIG_ARCH_MX7) || \
  74. defined(CONFIG_ARCH_MX7ULP) || defined(CONFIG_ARCH_IMX8M)
  75. hab_caam_clock_enable(1);
  76. u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR +
  77. FSL_CAAM_ORSR_JRa_OFFSET);
  78. if (out_jr_size != FSL_CAAM_MAX_JR_SIZE)
  79. sec_init();
  80. #endif
  81. if (enc)
  82. ret = blob_encap(km_ptr, src_ptr, dst_ptr, len);
  83. else
  84. ret = blob_decap(km_ptr, src_ptr, dst_ptr, len);
  85. return ret;
  86. }
  87. /***************************************************/
  88. static char blob_help_text[] =
  89. "enc src dst len km - Encapsulate and create blob of data\n"
  90. " $len bytes long at address $src and\n"
  91. " store the result at address $dst.\n"
  92. " $km is the address where the key\n"
  93. " modifier is stored.\n"
  94. " The modifier is required for generation\n"
  95. " /use as key for cryptographic operation.\n"
  96. " Key modifier should be 16 byte long.\n"
  97. "blob dec src dst len km - Decapsulate the blob of data at address\n"
  98. " $src and store result of $len byte at\n"
  99. " addr $dst.\n"
  100. " $km is the address where the key\n"
  101. " modifier is stored.\n"
  102. " The modifier is required for generation\n"
  103. " /use as key for cryptographic operation.\n"
  104. " Key modifier should be 16 byte long.\n";
  105. U_BOOT_CMD(
  106. blob, 6, 1, do_blob,
  107. "Blob encapsulation/decryption",
  108. blob_help_text
  109. );