yocto: improve bootloader_message [1/1]

PD#SWPL-99157

Problem:
improve bootloader_message

Solution:
improve bootloader_message

Verify:
S4_AP222

Change-Id: I8b1c353252aeb457f3a89685b27d84eb75134271
Signed-off-by: Bing Jiang <bing.jiang@amlogic.com>
diff --git a/bootloader_message/bootloader_avb.c b/bootloader_message/bootloader_avb.c
index 626e91d..cc9663b 100644
--- a/bootloader_message/bootloader_avb.c
+++ b/bootloader_message/bootloader_avb.c
@@ -1,8 +1,78 @@
 #include <errno.h>
-#include "bootloader_avb.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdbool.h>
+#include <stdint.h>
+
+#include "bootloader_message.h"
+
+#define AB_METADATA_MISC_PARTITION_OFFSET 2048
+
+#define MISCBUF_SIZE  2080
+
+
+/* Magic for the A/B struct when serialized. */
+#define AVB_AB_MAGIC "\0AB0"
+#define AVB_AB_MAGIC_LEN 4
+
+/* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */
+#define AVB_AB_MAJOR_VERSION 1
+#define AVB_AB_MINOR_VERSION 0
+
+/* Size of AvbABData struct. */
+#define AVB_AB_DATA_SIZE 32
+
+/* Maximum values for slot data */
+#define AVB_AB_MAX_PRIORITY 15
+#define AVB_AB_MAX_TRIES_REMAINING 7
+/* Struct used for recording per-slot metadata.
+ *
+ * When serialized, data is stored in network byte-order.
+ */
+typedef struct AvbABSlotData {
+  /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY,
+   * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY
+   * being the highest. The special value 0 is used to indicate the
+   * slot is unbootable.
+   */
+  uint8_t priority;
+
+  /* Number of times left attempting to boot this slot ranging from 0
+   * to AVB_AB_MAX_TRIES_REMAINING.
+   */
+  uint8_t tries_remaining;
+
+  /* Non-zero if this slot has booted successfully, 0 otherwise. */
+  uint8_t successful_boot;
+
+  /* Reserved for future use. */
+  uint8_t reserved[1];
+} AvbABSlotData;
+
+/* Struct used for recording A/B metadata.
+ *
+ * When serialized, data is stored in network byte-order.
+ */
+typedef struct AvbABData {
+  /* Magic number used for identification - see AVB_AB_MAGIC. */
+  uint8_t magic[AVB_AB_MAGIC_LEN];
+
+  /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */
+  uint8_t version_major;
+  uint8_t version_minor;
+
+  /* Padding to ensure |slots| field start eight bytes in. */
+  uint8_t reserved1[2];
+
+  /* Per-slot metadata. */
+  AvbABSlotData slots[2];
+
+  /* Reserved for future use. */
+  uint8_t reserved2[12];
+
+  /* CRC32 of all 28 bytes preceding this field. */
+  uint32_t crc32;
+}AvbABData;
 
 #ifdef BOOTCTOL_AVB
 static void dump_boot_info(AvbABData* info)
@@ -153,7 +223,7 @@
     info->slots[1].successful_boot = 0;
 }
 
-int set_active_slot(int slot) {
+int set_active_slot(SlotType_e slot) {
     int ret = 0;
     char miscbuf[MISCBUF_SIZE] = {0};
     AvbABData info;
diff --git a/bootloader_message/bootloader_avb.h b/bootloader_message/bootloader_avb.h
deleted file mode 100644
index f19b655..0000000
--- a/bootloader_message/bootloader_avb.h
+++ /dev/null
@@ -1,76 +0,0 @@
-#ifndef _BOOTLOADER_AVB_H_
-#define _BOOTLOADER_AVB_H_
-
-
-#include <stdint.h>
-
-#define AB_METADATA_MISC_PARTITION_OFFSET 2048
-
-#define MISCBUF_SIZE  2080
-
-
-/* Magic for the A/B struct when serialized. */
-#define AVB_AB_MAGIC "\0AB0"
-#define AVB_AB_MAGIC_LEN 4
-
-/* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */
-#define AVB_AB_MAJOR_VERSION 1
-#define AVB_AB_MINOR_VERSION 0
-
-/* Size of AvbABData struct. */
-#define AVB_AB_DATA_SIZE 32
-
-/* Maximum values for slot data */
-#define AVB_AB_MAX_PRIORITY 15
-#define AVB_AB_MAX_TRIES_REMAINING 7
-/* Struct used for recording per-slot metadata.
- *
- * When serialized, data is stored in network byte-order.
- */
-typedef struct AvbABSlotData {
-  /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY,
-   * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY
-   * being the highest. The special value 0 is used to indicate the
-   * slot is unbootable.
-   */
-  uint8_t priority;
-
-  /* Number of times left attempting to boot this slot ranging from 0
-   * to AVB_AB_MAX_TRIES_REMAINING.
-   */
-  uint8_t tries_remaining;
-
-  /* Non-zero if this slot has booted successfully, 0 otherwise. */
-  uint8_t successful_boot;
-
-  /* Reserved for future use. */
-  uint8_t reserved[1];
-} AvbABSlotData;
-
-/* Struct used for recording A/B metadata.
- *
- * When serialized, data is stored in network byte-order.
- */
-typedef struct AvbABData {
-  /* Magic number used for identification - see AVB_AB_MAGIC. */
-  uint8_t magic[AVB_AB_MAGIC_LEN];
-
-  /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */
-  uint8_t version_major;
-  uint8_t version_minor;
-
-  /* Padding to ensure |slots| field start eight bytes in. */
-  uint8_t reserved1[2];
-
-  /* Per-slot metadata. */
-  AvbABSlotData slots[2];
-
-  /* Reserved for future use. */
-  uint8_t reserved2[12];
-
-  /* CRC32 of all 28 bytes preceding this field. */
-  uint32_t crc32;
-}AvbABData;
-
-#endif
-
diff --git a/bootloader_message/bootloader_message.c b/bootloader_message/bootloader_message.c
index 0c36cf5..2976ac2 100644
--- a/bootloader_message/bootloader_message.c
+++ b/bootloader_message/bootloader_message.c
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <stdint.h>
 #include <mtd/mtd-user.h>
+#include <stdint.h>
 
 #include "bootloader_message.h"
 
@@ -23,6 +24,56 @@
 #define CMD_RUN_RECOVERY   "boot-recovery"
 #define MTD_PROC_FILENAME   "/proc/mtd"
 
+struct bootloader_message {
+    char command[32];
+    char status[32];
+    char recovery[768];
+
+    // The 'recovery' field used to be 1024 bytes.  It has only ever
+    // been used to store the recovery command line, so 768 bytes
+    // should be plenty.  We carve off the last 256 bytes to store the
+    // stage string (for multistage packages) and possible future
+    // expansion.
+    char stage[32];
+    char slot_suffix[32];
+    char reserved[192];
+};
+
+typedef struct BrilloSlotInfo {
+    uint8_t bootable;
+    uint8_t online;
+    uint8_t reserved[2];
+} BrilloSlotInfo;
+
+typedef struct BrilloBootInfo {
+    // Used by fs_mgr. Must be NUL terminated.
+    char bootctrl_suffix[4];
+
+    // Magic for identification - must be 'B', 'C', 'c' (short for
+    // "boot_control copy" implementation).
+    uint8_t magic[3];
+
+    // Version of BrilloBootInfo struct, must be 0 or larger.
+    uint8_t version;
+
+    // Currently active slot.
+    uint8_t active_slot;
+
+    // Information about each slot.
+    BrilloSlotInfo slot_info[2];
+    uint8_t attemp_times;
+
+    uint8_t reserved[14];
+} BrilloBootInfo;
+
+typedef struct MtdPartition {
+    int device_index;
+    long long size;
+    long long erase_size;
+    char *name;
+} MtdPartition;
+
+
 int g_init = 0;
 int g_mtd_number = 0;
 MtdPartition mtdpartition[32] = {0};
@@ -741,16 +792,16 @@
 }
 #endif
 
-int get_active_slot(int *slot) {
+int get_active_slot(SlotType_e *slot) {
     return get_active_slot_from_cmdline(slot);
 }
 
-int get_active_slot_misc(int *slot) {
+int get_active_slot_misc(SlotType_e *slot) {
     return get_active_slot_from_misc(slot);
 }
 
 #ifndef BOOTCTOL_AVB
-int set_active_slot(int slot) {
+int set_active_slot(SlotType_e slot) {
     int ret = 0;
     struct bootloader_message info;
     BrilloBootInfo bootinfo;
@@ -781,7 +832,7 @@
 }
 #endif
 
-int get_inactive_devicename(const char *partitionname, int slot, char *device) {
+int get_inactive_devicename(const char *partitionname, SlotType_e slot, char *device) {
     int ret = 0;
     int mtd = 0;
     char tmp[128] = {0};
diff --git a/bootloader_message/bootloader_message.h b/bootloader_message/bootloader_message.h
index 2690bcb..aafa397 100644
--- a/bootloader_message/bootloader_message.h
+++ b/bootloader_message/bootloader_message.h
@@ -1,73 +1,33 @@
+#ifndef _AML_BOOTLOADER_MESSAGE_H
+#define _AML_BOOTLOADER_MESSAGE_H
 
-#ifndef _INIT_BOOTLOADER_MESSAGE_H
-#define _INIT_BOOTLOADER_MESSAGE_H
+#ifdef __cplusplus
+extern "C" {
+#endif
 
-#include <stdint.h>
+typedef enum {
+  SLOT_A,
+  SLOT_B,
+} SlotType_e;
 
+int get_active_slot(SlotType_e *slot);
+int set_active_slot(SlotType_e slot);
+int get_active_slot_misc(SlotType_e *slot);
+int set_successful_boot();
 
-struct bootloader_message {
-    char command[32];
-    char status[32];
-    char recovery[768];
-
-    // The 'recovery' field used to be 1024 bytes.  It has only ever
-    // been used to store the recovery command line, so 768 bytes
-    // should be plenty.  We carve off the last 256 bytes to store the
-    // stage string (for multistage packages) and possible future
-    // expansion.
-    char stage[32];
-    char slot_suffix[32];
-    char reserved[192];
-};
-
-typedef struct BrilloSlotInfo {
-    uint8_t bootable;
-    uint8_t online;
-    uint8_t reserved[2];
-} BrilloSlotInfo;
-
-typedef struct BrilloBootInfo {
-    // Used by fs_mgr. Must be NUL terminated.
-    char bootctrl_suffix[4];
-
-    // Magic for identification - must be 'B', 'C', 'c' (short for
-    // "boot_control copy" implementation).
-    uint8_t magic[3];
-
-    // Version of BrilloBootInfo struct, must be 0 or larger.
-    uint8_t version;
-
-    // Currently active slot.
-    uint8_t active_slot;
-
-    // Information about each slot.
-    BrilloSlotInfo slot_info[2];
-    uint8_t attemp_times;
-
-    uint8_t reserved[14];
-} BrilloBootInfo;
-
-typedef struct MtdPartition {
-    int device_index;
-    long long size;
-    long long erase_size;
-    char *name;
-} MtdPartition;
-
-int reboot_recovery();
 int clear_recovery();
 int set_recovery();
-int get_active_slot(int *slot);
-int set_active_slot(int slot);
-int get_active_slot_misc(int *slot);
-int set_successful_boot();
 int set_recovery_otapath(char *path);
 int get_recovery_otapath(char * path);
 int clean_recovery_otapath();
+
 int get_inactive_mtd(const char *partitionname);
-int get_inactive_devicename(const char *partitionname, int slot, char *device);
+int get_inactive_devicename(const char *partitionname, SlotType_e slot, char *device);
 int get_system_type();
 int mtd_scan_partitions();
 
+#ifdef __cplusplus
+} /* extern "C" */
 #endif
 
+#endif