rom_sys.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*!
  2. \file rom_sys.h
  3. \brief Rom system file for GD32VW55x SDK
  4. \version 2023-07-20, V1.0.0, firmware for GD32VW55x
  5. */
  6. /*
  7. Copyright (c) 2023, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #ifndef __ROM_SYS_H__
  30. #define __ROM_SYS_H__
  31. #define SYS_STATUS_ENCRPTED 1
  32. /* ============================================================= */
  33. /* System setting related */
  34. #define SYS_SETTING_MAGIC 0x47443332 /* "GD32" */
  35. #define MAX_VER_MAJOR 254 // 16
  36. #define MAX_VER_MINOR 254 // 16
  37. struct sys_setting_t {
  38. uint32_t rsvd0;
  39. uint32_t rsvd1;
  40. uint32_t sys_magic; /* Magic: 0x47443332,"GD32" */
  41. uint32_t sysset_offset; /* the flash offset of System Settings */
  42. uint32_t mbl_offset; /* the flash offset of MBL binary */
  43. uint32_t sysstatus_offset; /* the flash offset of System Status */
  44. uint32_t img0_offset; /* the flash offset of Main Image 0 */
  45. uint32_t img1_offset; /* the flash offset of Main Image 1 */
  46. uint32_t mbl_initial_version;
  47. uint32_t img_initial_version;
  48. uint32_t ver_locked;
  49. uint32_t flash_totsz; /* flash total size */
  50. uint32_t rsvd[]; /* Reserved for Flash parameters or ADC parameters */
  51. };
  52. /* ============================================================= */
  53. /* System status related */
  54. #define SYS_STATUS_AREA_SZ 0x1000
  55. #define SYS_STATUS_HEADER_SZ (sizeof(struct sys_status_header_t))
  56. #define SYS_STATUS_TLV_HEADER_SZ 2
  57. #define MAX_TLV_VALUE_SIZE 16
  58. #define SYS_STATUS_MAGIC_CODE 0x19342750
  59. #define AES_KEY_SZ 16
  60. #define AES_BLOCK_SZ AES_KEY_SZ
  61. /* System status error definitions */
  62. #define SYS_STATUS_OK 0
  63. #define SYS_STATUS_ERR_FLASH 1
  64. #define SYS_STATUS_ERR_KEY (1 << 1)
  65. #define SYS_STATUS_ERR_TOTAL_LEN (1 << 2)
  66. #define SYS_STATUS_ERR_MAGIC (1 << 3)
  67. #define SYS_STATUS_ERR_CHECKSUM (1 << 4)
  68. #define SYS_STATUS_ERR_TLV (1 << 5)
  69. #define SYS_STATUS_ERR_CRYPT (1 << 6)
  70. #define SYS_STATUS_ERR_MEM (1 << 7)
  71. #define SYS_STATUS_ERR_INPUT (1 << 8)
  72. /* System status item length */
  73. #define LEN_SYS_ERROR_PROCESS 1
  74. #define LEN_SYS_TRACE_LEVEL 1
  75. #define LEN_SYS_IMAGE_STATUS 1
  76. #define LEN_SYS_RUNNING_IMG 1
  77. #define LEN_SYS_VER_COUNTER 4
  78. #define LEN_SYS_PKVER_COUNTER 1
  79. #define LEN_SYS_TRNG_SEED 1
  80. //#define LEN_SYS_UNLOCK_COUNTER 2
  81. #define LEN_SYS_AUTO_CONN 1
  82. #define LEN_SYS_SSID_X 16
  83. #define LEN_SYS_PASSWD_X 16
  84. #define LEN_SYS_IP_ADDR 4
  85. /* ERROR process */
  86. #define ERR_PROCESS_WAIT_DOWNLOAD 0
  87. #define ERR_PROCESS_ENDLESS_LOOP 1
  88. /* JTAG status */
  89. #define JTAG_UNLOCKED 0
  90. #define JTAG_LOCKED 1
  91. /* SYS status found result */
  92. #define SYS_STATUS_FOUND_OK 0
  93. #define SYS_STATUS_NOT_FOUND -1
  94. #define SYS_STATUS_FOUND_ERR -2
  95. /* Image Index */
  96. #define IMAGE_0 0
  97. #define IMAGE_1 1
  98. /*
  99. * Public Key TYPE.
  100. */
  101. #define PK_TYPE_MBL 0x1
  102. /* There are two flash blocks used to manage system status: Ping, Pong.
  103. The format of system status is shown as below.
  104. | total size | valid count | Magic | Checksum |
  105. | type | len | value|
  106. valid count - used to indicate which is active, Ping or Pong. A higher valid count represents the active system status.
  107. total size - the total size of the following TLVs
  108. */
  109. enum {
  110. SYS_ERROR_PROCESS = 1, /* 0: Enter UART Download; 1: while(1) */
  111. SYS_TRACE_LEVEL = 2, /* The Trace Level for IBL and MBL */
  112. SYS_IMAGE0_STATUS = 3, /* the status of Main Image 0, enum image_status */
  113. SYS_IMAGE1_STATUS = 4, /* the status of Main Image 1, enum image_status */
  114. SYS_RUNNING_IMG = 5, /* 0: Image0, 1: Image1 */
  115. SYS_MBL_VER_COUNTER = 6, /* NV counter for MBL version */
  116. SYS_IMG_VER_COUNTER = 7, /* NV counter for Image version */
  117. SYS_MBLPK_VER_COUNTER = 8, /* NV counter for MBL Public Key version */
  118. //SYS_JTAG_UNLOCK_COUNTER = 9, /* NV counter for JTAG unlock error */
  119. //SYS_JTAG_STATUS = 10, /* JTAG Lock Status. 0: unlock, 1: lock */
  120. SYS_MAX_ROM_TYPE = 0xF,
  121. /* Defined by Application */
  122. SYS_AUTO_CONN,
  123. SYS_SSID_1,
  124. SYS_SSID_2,
  125. SYS_PASSWD_1,
  126. SYS_PASSWD_2,
  127. SYS_PASSWD_3,
  128. SYS_PASSWD_4,
  129. SYS_IP_ADDR,
  130. SYS_UNKNOWN_TYPE = 0xFF
  131. };
  132. #if (SYS_STATUS_ENCRPTED == 1)
  133. struct sys_status_header_t {
  134. uint32_t tot_len;
  135. uint32_t act_cntr;
  136. uint32_t magic;
  137. uint32_t checksum;
  138. };
  139. #else
  140. struct sys_status_header_t {
  141. uint32_t act_cntr;
  142. uint32_t tot_len;
  143. };
  144. #endif
  145. extern int32_t err_process;
  146. extern uint32_t flash_offset_sys_status;
  147. /* ============================================================= */
  148. /* Sys APIs */
  149. int sys_setting_get_internal(void *settings);
  150. void sys_setting_show(void);
  151. // int sys_setting_get_version(uint32_t type, uint8_t *major, uint8_t *minor);
  152. // int sys_setting_set_version(uint32_t type, uint8_t major, uint8_t minor);
  153. int sys_status_init(void);
  154. int sys_status_check_integrity(void);
  155. int sys_status_set_internal(uint8_t type, uint8_t len, uint8_t* pval);
  156. int sys_status_get_internal(uint8_t type, uint8_t len, uint8_t* pval);
  157. int sys_set_fw_version_internal(uint32_t type, uint32_t version);
  158. void sys_status_show(void);
  159. void sys_status_dump(int is_ping);
  160. int is_valid_image_status(uint8_t status);
  161. //int sys_jtag_challenge(uint8_t *plaintext, uint8_t *ciphertext);
  162. #ifdef ROM_SELF_TEST
  163. void sys_status_self_test(void);
  164. #endif
  165. #endif //__ROM_SYS_H__