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