bootctrl.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein is
  5. * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
  6. * the prior written permission of MediaTek inc. and/or its licensors, any
  7. * reproduction, modification, use or disclosure of MediaTek Software, and
  8. * information contained herein, in whole or in part, shall be strictly
  9. * prohibited.
  10. *
  11. * MediaTek Inc. (C) 2016. All rights reserved.
  12. *
  13. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  14. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  15. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
  16. * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
  17. * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  19. * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
  20. * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
  21. * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
  22. * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
  23. * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
  24. * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
  25. * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
  26. * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  27. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
  28. * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
  29. * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
  30. * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
  31. * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  32. *
  33. * The following software/firmware and/or related documentation ("MediaTek
  34. * Software") have been modified by MediaTek Inc. All revisions are subject to
  35. * any receiver's applicable license agreements with MediaTek Inc.
  36. */
  37. /* THE HAL BOOTCTRL HEADER MUST BE IN SYNC WITH THE UBOOT BOOTCTRL HEADER */
  38. #ifndef _BOOTCTRL_H_
  39. #define _BOOTCTRL_H_
  40. #include <stdint.h>
  41. /* struct boot_ctrl occupies the slot_suffix field of
  42. * struct bootloader_message */
  43. #define OFFSETOF_SLOT_SUFFIX 2048
  44. #define BOOTCTRL_SUFFIX_MAXLEN 2
  45. #define BOOTCTRL_MAGIC 0x42414342
  46. #define BOOTCTRL_VERSION 1
  47. #define BOOTCTRL_SUFFIX_A "_a"
  48. #define BOOTCTRL_SUFFIX_B "_b"
  49. #define READ_PARTITION 0
  50. #define WRITE_PARTITION 1
  51. #define BOOT_CONTROL_MAX_RETRY (7)
  52. #define BOOT_CONTROL_MAX_PRI (15)
  53. #define BOOTCTRL_NUM_SLOTS (2)
  54. struct slot_metadata {
  55. // Slot priority with 15 meaning highest priority, 1 lowest
  56. // priority and 0 the slot is unbootable.
  57. uint8_t priority : 4;
  58. // Number of times left attempting to boot this slot.
  59. uint8_t tries_remaining : 3;
  60. // 1 if this slot has booted successfully, 0 otherwise.
  61. uint8_t successful_boot : 1;
  62. // 1 if this slot is corrupted from a dm-verity corruption, 0
  63. // otherwise.
  64. uint8_t verity_corrupted : 1;
  65. // Reserved for further use.
  66. uint8_t reserved : 7;
  67. } __attribute__((packed));
  68. /* Bootloader Control AB
  69. *
  70. * This struct can be used to manage A/B metadata. It is designed to
  71. * be put in the 'slot_suffix' field of the 'bootloader_message'
  72. * structure described above. It is encouraged to use the
  73. * 'bootloader_control' structure to store the A/B metadata, but not
  74. * mandatory.
  75. */
  76. struct bootloader_control {
  77. // NUL terminated active slot suffix.
  78. char slot_suffix[4];
  79. // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC).
  80. uint32_t magic;
  81. // Version of struct being used (see BOOT_CTRL_VERSION).
  82. uint8_t version;
  83. // Number of slots being managed.
  84. uint8_t nb_slot : 3;
  85. // Number of times left attempting to boot recovery.
  86. uint8_t recovery_tries_remaining : 3;
  87. // Status of any pending snapshot merge of dynamic partitions.
  88. uint8_t merge_status : 3;
  89. // Ensure 4-bytes alignment for slot_info field.
  90. uint8_t reserved0[1];
  91. // Per-slot information. Up to 4 slots.
  92. struct slot_metadata slot_info[4];
  93. // Reserved for further use.
  94. uint8_t reserved1[8];
  95. // CRC32 of all 28 bytes preceding this field (little endian
  96. // format).
  97. uint32_t crc32_le;
  98. } __attribute__((packed));
  99. /* bootctrl API */
  100. const char *get_suffix(void);
  101. int set_active_slot(const char *suffix);
  102. uint8_t get_retry_count(const char *suffix);
  103. int get_bootup_status(const char *suffix);
  104. int get_bootable_status(const char *suffix);
  105. int increase_tries_remaining(void);
  106. int get_slot_info(int slot, uint8_t *priority, uint8_t *tries_remaining, uint8_t *successful_boot);
  107. int bootctrl_retry_decrease(int slot);
  108. #endif /* _BOOTCTRL_H_ */