cmd_dek.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2008-2015 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Command for encapsulating DEK blob
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <environment.h>
  11. #include <malloc.h>
  12. #include <asm/byteorder.h>
  13. #include <linux/compiler.h>
  14. #include <fsl_sec.h>
  15. #include <asm/arch/clock.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. /**
  18. * blob_dek() - Encapsulate the DEK as a blob using CAM's Key
  19. * @src: - Address of data to be encapsulated
  20. * @dst: - Desination address of encapsulated data
  21. * @len: - Size of data to be encapsulated
  22. *
  23. * Returns zero on success,and negative on error.
  24. */
  25. static int blob_encap_dek(const u8 *src, u8 *dst, u32 len)
  26. {
  27. int ret = 0;
  28. u32 jr_size = 4;
  29. u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR + 0x102c);
  30. if (out_jr_size != jr_size) {
  31. hab_caam_clock_enable(1);
  32. sec_init();
  33. }
  34. if (!((len == 128) | (len == 192) | (len == 256))) {
  35. debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
  36. return -1;
  37. }
  38. len /= 8;
  39. ret = blob_dek(src, dst, len);
  40. return ret;
  41. }
  42. /**
  43. * do_dek_blob() - Handle the "dek_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_dek_blob(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  53. {
  54. uint32_t src_addr, dst_addr, len;
  55. uint8_t *src_ptr, *dst_ptr;
  56. int ret = 0;
  57. if (argc != 4)
  58. return CMD_RET_USAGE;
  59. src_addr = simple_strtoul(argv[1], NULL, 16);
  60. dst_addr = simple_strtoul(argv[2], NULL, 16);
  61. len = simple_strtoul(argv[3], NULL, 10);
  62. src_ptr = map_sysmem(src_addr, len/8);
  63. dst_ptr = map_sysmem(dst_addr, BLOB_SIZE(len/8));
  64. ret = blob_encap_dek(src_ptr, dst_ptr, len);
  65. return ret;
  66. }
  67. /***************************************************/
  68. static char dek_blob_help_text[] =
  69. "src dst len - Encapsulate and create blob of data\n"
  70. " $len bits long at address $src and\n"
  71. " store the result at address $dst.\n";
  72. U_BOOT_CMD(
  73. dek_blob, 4, 1, do_dek_blob,
  74. "Data Encryption Key blob encapsulation",
  75. dek_blob_help_text
  76. );