cmd_dek.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <environment.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(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  52. {
  53. uint32_t src_addr, dst_addr, len;
  54. uint8_t *src_ptr, *dst_ptr;
  55. int ret = 0;
  56. if (argc != 4)
  57. return CMD_RET_USAGE;
  58. src_addr = simple_strtoul(argv[1], NULL, 16);
  59. dst_addr = simple_strtoul(argv[2], NULL, 16);
  60. len = simple_strtoul(argv[3], NULL, 10);
  61. src_ptr = map_sysmem(src_addr, len/8);
  62. dst_ptr = map_sysmem(dst_addr, BLOB_SIZE(len/8));
  63. ret = blob_encap_dek(src_ptr, dst_ptr, len);
  64. return ret;
  65. }
  66. /***************************************************/
  67. static char dek_blob_help_text[] =
  68. "src dst len - Encapsulate and create blob of data\n"
  69. " $len bits long at address $src and\n"
  70. " store the result at address $dst.\n";
  71. U_BOOT_CMD(
  72. dek_blob, 4, 1, do_dek_blob,
  73. "Data Encryption Key blob encapsulation",
  74. dek_blob_help_text
  75. );