imagef.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #ifndef __IMAGE_F_H__
  7. #define __IMAGE_F_H__
  8. #define IMG_NAME_MAX_LEN 16
  9. #define IMG_PATH_MAX_LEN 24
  10. #define IMG_MAX_COUNT 8
  11. #define DEV_NAME_MAX_LEN 16
  12. typedef enum {
  13. DIGEST_HASH_NONE = 0,
  14. DIGEST_HASH_SHA1 = 1,
  15. DIGEST_HASH_MD5 = 2,
  16. DIGEST_HASH_SHA224 = 3,
  17. DIGEST_HASH_SHA256 = 4,
  18. DIGEST_HASH_SHA384 = 5,
  19. DIGEST_HASH_SHA512 = 6,
  20. DIGEST_HASH_SM3 = 7,
  21. DIGEST_HASH_TYPE_END
  22. } digest_sch_e;
  23. typedef enum {
  24. SIGNATURE_NONE = 0,
  25. SIGNATURE_RSA_1024 = 1,
  26. SIGNATURE_RSA_2048 = 2,
  27. SIGNATURE_ECC_256 = 3,
  28. SIGNATURE_ECC_160 = 4,
  29. SIGNATURE_SM2 = 5,
  30. SIGNATURE_AES_128_CCM = 10,
  31. SIGNATURE_TYPE_END
  32. } signature_sch_e;
  33. typedef struct {
  34. char img_name[IMG_NAME_MAX_LEN];
  35. uint32_t offset;
  36. uint32_t size;
  37. } pack_header_imginfo_v2_t;
  38. typedef struct {
  39. #define PACK_IMG_MAX_COUNT 15U
  40. #define PACK_HEAD_MAGIC 0x4B434150
  41. uint32_t magic; // "PACK" 0x4B434150
  42. uint16_t head_version; // 2
  43. uint16_t head_size; // length of header
  44. uint32_t head_checksum; // the checksum for header, fill 0 when calculate checksum
  45. uint32_t image_count; // image count to pack
  46. unsigned char md5sum[16]; // the md5sum for md5sum(image1+image2+image3...)
  47. uint16_t digest_type; // the digest type
  48. uint16_t signature_type; // the signature type
  49. unsigned char signature[512]; // the signature for header + image, fill 0 when calculate checksum or calculate signature
  50. uint32_t rsv[29]; // reverse
  51. pack_header_imginfo_v2_t image_info[PACK_IMG_MAX_COUNT]; // 24*15=360B
  52. } pack_header_v2_t; // 1024Bytes
  53. typedef struct {
  54. uint32_t image_count;
  55. size_t head_size;
  56. unsigned char md5sum[16];
  57. uint16_t digest_type;
  58. uint16_t signature_type;
  59. unsigned char signature[512];
  60. struct {
  61. FILE *fp;
  62. int fd;
  63. char img_name[IMG_NAME_MAX_LEN];
  64. char img_path[IMG_PATH_MAX_LEN];
  65. char dev_name[DEV_NAME_MAX_LEN];
  66. size_t img_offset;
  67. size_t img_size;
  68. size_t partition_size;
  69. size_t write_size;
  70. size_t read_size;
  71. } img_info[IMG_MAX_COUNT];
  72. } download_img_info_t;
  73. #define IMG_NAME_UBOOT "uboot"
  74. #define IMG_NAME_KERNEL "kernel"
  75. #define IMG_NAME_ROOTFS "rootfs"
  76. #define IMG_NAME_TF "tf"
  77. #define IMG_NAME_TEE "tee"
  78. #define IMG_NAME_TFSTASH "tfstash"
  79. #define IMG_NAME_TEESTASH "teestash"
  80. #define IMG_NAME_DIFF "diff"
  81. #define IMGINFOFILE "/fotaimgsinfo.bin" // save download_img_info_t
  82. #define IMGHEADERPATH "/fotaimgsheader.bin" // save pack_header_v2_t, because of signature verify need header raw data.
  83. int get_file_size(FILE *fp, int fd);
  84. uint32_t get_checksum(uint8_t *data, uint32_t length);
  85. char *strdup_img_path(const char *img_name);
  86. int check_kernel_partition(void);
  87. int check_rootfs_partition(void);
  88. int check_partition_ab(const char *name);
  89. int get_rootfs_file_system_type(void);
  90. #define FILE_SYSTEM_IS_UBI() (get_rootfs_file_system_type() == 1)
  91. #define FILE_SYSTEM_IS_EXT4() (get_rootfs_file_system_type() == 2)
  92. int mbed_sha1_rsa_verify(uint8_t *pub_key, uint32_t key_size, uint8_t *hash, uint8_t *sig);
  93. int mbed_sha256_rsa_verify(uint8_t *pub_key, uint32_t key_size, uint8_t *hash, uint8_t *sig);
  94. int set_rollback_env_param(int limit_c);
  95. int set_fota_success_param(void);
  96. int set_ubivol_cmd(const char* vol_name, char *o_cmd, int len);
  97. static inline int check_partition_exists(const char *name)
  98. {
  99. const char *p[] = {"uboot", "boot", "root",
  100. IMG_NAME_TF, IMG_NAME_TEE, IMG_NAME_TFSTASH, IMG_NAME_TEESTASH};
  101. int i;
  102. for (i = 0; i < sizeof(p) / sizeof(p[0]); i++) {
  103. if (!strcmp(name, p[i])) {
  104. return 1;
  105. }
  106. }
  107. return 0;
  108. }
  109. static inline const char *read_partition_img_name(const char *name)
  110. {
  111. const char *p[] = {"uboot", "boot", "root",
  112. IMG_NAME_TF, IMG_NAME_TEE, IMG_NAME_TFSTASH, IMG_NAME_TEESTASH};
  113. const char *p2[] = {IMG_NAME_UBOOT, IMG_NAME_KERNEL, IMG_NAME_ROOTFS,
  114. IMG_NAME_TF, IMG_NAME_TEE, IMG_NAME_TFSTASH, IMG_NAME_TEESTASH};
  115. int i;
  116. for (i = 0; i < sizeof(p) / sizeof(p[0]); i++) {
  117. if (!strcmp(name, p[i])) {
  118. return p2[i];
  119. }
  120. }
  121. return NULL;
  122. }
  123. #endif