cmd_dek.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <mapmem.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /**
  19. * blob_dek() - Encapsulate the DEK as a blob using CAM's Key
  20. * @src: - Address of data to be encapsulated
  21. * @dst: - Desination address of encapsulated data
  22. * @len: - Size of data to be encapsulated
  23. *
  24. * Returns zero on success,and negative on error.
  25. */
  26. static int blob_encap_dek(const u8 *src, u8 *dst, u32 len)
  27. {
  28. int ret = 0;
  29. u32 jr_size = 4;
  30. u32 out_jr_size = sec_in32(CONFIG_SYS_FSL_JR0_ADDR + 0x102c);
  31. if (out_jr_size != jr_size) {
  32. hab_caam_clock_enable(1);
  33. sec_init();
  34. }
  35. if (!((len == 128) | (len == 192) | (len == 256))) {
  36. debug("Invalid DEK size. Valid sizes are 128, 192 and 256b\n");
  37. return -1;
  38. }
  39. len /= 8;
  40. ret = blob_dek(src, dst, len);
  41. return ret;
  42. }
  43. /**
  44. * do_dek_blob() - Handle the "dek_blob" command-line command
  45. * @cmdtp: Command data struct pointer
  46. * @flag: Command flag
  47. * @argc: Command-line argument count
  48. * @argv: Array of command-line arguments
  49. *
  50. * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  51. * on error.
  52. */
  53. static int do_dek_blob(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  54. {
  55. uint32_t src_addr, dst_addr, len;
  56. uint8_t *src_ptr, *dst_ptr;
  57. int ret = 0;
  58. if (argc != 4)
  59. return CMD_RET_USAGE;
  60. src_addr = simple_strtoul(argv[1], NULL, 16);
  61. dst_addr = simple_strtoul(argv[2], NULL, 16);
  62. len = simple_strtoul(argv[3], NULL, 10);
  63. src_ptr = map_sysmem(src_addr, len/8);
  64. dst_ptr = map_sysmem(dst_addr, BLOB_SIZE(len/8));
  65. ret = blob_encap_dek(src_ptr, dst_ptr, len);
  66. return ret;
  67. }
  68. /***************************************************/
  69. static char dek_blob_help_text[] =
  70. "src dst len - Encapsulate and create blob of data\n"
  71. " $len bits long at address $src and\n"
  72. " store the result at address $dst.\n";
  73. U_BOOT_CMD(
  74. dek_blob, 4, 1, do_dek_blob,
  75. "Data Encryption Key blob encapsulation",
  76. dek_blob_help_text
  77. );