cmd_dek.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2008-2015 Freescale Semiconductor, Inc.
  4. *
  5. * Command for encapsulating DEK blob
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/compiler.h>
  13. #include <fsl_sec.h>
  14. #include <asm/arch/clock.h>
  15. #include <mapmem.h>
  16. /**
  17. * blob_dek() - Encapsulate the DEK as a blob using CAM's Key
  18. * @src: - Address of data to be encapsulated
  19. * @dst: - Desination address of encapsulated data
  20. * @len: - Size of data to be encapsulated
  21. *
  22. * Returns zero on success,and negative on error.
  23. */
  24. static int blob_encap_dek(const u8 *src, u8 *dst, u32 len)
  25. {
  26. int ret = 0;
  27. u32 jr_size = 4;
  28. u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR + 0x102c);
  29. if (out_jr_size != jr_size) {
  30. hab_caam_clock_enable(1);
  31. sec_init();
  32. }
  33. if (!((len == 128) | (len == 192) | (len == 256))) {
  34. debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
  35. return -1;
  36. }
  37. len /= 8;
  38. ret = blob_dek(src, dst, len);
  39. return ret;
  40. }
  41. /**
  42. * do_dek_blob() - Handle the "dek_blob" command-line command
  43. * @cmdtp: Command data struct pointer
  44. * @flag: Command flag
  45. * @argc: Command-line argument count
  46. * @argv: Array of command-line arguments
  47. *
  48. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  49. * on error.
  50. */
  51. static int do_dek_blob(struct cmd_tbl *cmdtp, int flag, int argc,
  52. 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. );