Add glibc support(default)
Change-Id: I7675edcf14df8707ecd424a962e4cc464a4c6ae4
diff --git a/mbtk/aboot-tiny/include/aboot-tiny.h b/mbtk/aboot-tiny/include/aboot-tiny.h
new file mode 100755
index 0000000..9e9a72c
--- /dev/null
+++ b/mbtk/aboot-tiny/include/aboot-tiny.h
@@ -0,0 +1,242 @@
+/**
+ * @file
+ * @author Jinhua Huang <jinhuahuang@asrmicro.com>
+ * @version 1.0
+ *
+ * @section LICENSE
+ * Copyright (C) 2020, ASR microelectronics, All rights reserved.
+ *
+ * @section DESCRIPTION
+ *
+ * The time class represents a moment of time.
+ */
+#ifndef ABOOT_TINY_H
+#define ABOOT_TINY_H
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#ifdef __linux__
+#include <sys/select.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined _WIN32 || defined __CYGWIN__ || defined __MINGW32__
+#if defined(BUILD_DLL)
+#define ABOOT_API __declspec(dllexport)
+#elif defined(USE_DLL)
+#define ABOOT_API __declspec(dllimport)
+#else
+#define ABOOT_API
+#endif
+#else
+#define ABOOT_API __attribute__ ((visibility("default")))
+#endif
+
+/**
+ * Callback events to upper layer
+ */
+typedef enum {
+ ABOOT_TINY_EVENT_START,
+ ABOOT_TINY_EVENT_DOWNLOAD,
+ ABOOT_TINY_EVENT_STOP,
+ ABOOT_TINY_EVENT_PROGRESS,
+ ABOOT_TINY_EVENT_STATUS,
+} aboot_tiny_event_t;
+
+/**
+ * Aboot device running status
+ */
+#define ABOOT_TINY_STATUS_CONNECTING "CONNECTING"
+#define ABOOT_TINY_STATUS_RUNNING "RUNNING"
+#define ABOOT_TINY_STATUS_FAILED "FAILED"
+#define ABOOT_TINY_STATUS_SUCCEEDED "SUCCEEDED"
+
+/**
+ * Aboot command/response size definition
+ */
+#define ABOOT_COMMAND_SZ 128
+#define ABOOT_RESPONSE_SZ 128
+
+/**
+ * Aboot error code definition
+ */
+typedef enum {
+ ABOOT_TINY_ERROR_SUCCESS,
+ ABOOT_TINY_ERROR_PREAMBLE_TIMEOUT,
+ ABOOT_TINY_ERROR_TRANSPORT_INIT_FAILED,
+ ABOOT_TINY_ERROR_LINK_LOST,
+ ABOOT_TINY_ERROR_NO_RESPONSE,
+ ABOOT_TINY_ERROR_HANDLE_GETVAR,
+ ABOOT_TINY_ERROR_HANDLE_DOWNLOAD,
+ ABOOT_TINY_ERROR_READ_DATA,
+ ABOOT_TINY_ERROR_UNEXPECT_RESPONSE,
+ ABOOT_TINY_ERROR_FORCE_STOP,
+} aboot_tiny_error_t;
+
+/**
+ * Callback message to upper layer
+ */
+typedef struct {
+ aboot_tiny_event_t event;
+ aboot_tiny_error_t error;
+ union {
+ const char *message;
+ int progress;
+ const char *status;
+ } u;
+} aboot_tiny_message_t;
+
+/**
+ * Callback function pointer type
+ *
+ * @param msg callback info
+ *
+ */
+typedef void (*aboot_tiny_callback_t)(const aboot_tiny_message_t *msg);
+
+/**
+ * Function poiner type for customization layer interface
+ */
+typedef struct firmware_handle firmware_handle_t;
+typedef int (*aboot_tiny_uart_send_t)(const uint8_t *data, size_t size);
+typedef int (*aboot_tiny_firmware_read_line_t)(firmware_handle_t *handle,
+ char *data);
+typedef int (*aboot_tiny_firmware_read_data_t)(firmware_handle_t *handle,
+ uint8_t *data, size_t size);
+typedef unsigned (*aboot_tiny_clock_get_t)(void);
+typedef size_t (*aboot_tiny_uart_rx_callback_t)(const uint8_t *data,
+ size_t size);
+typedef void *(*aboot_tiny_mem_alloc_t)(size_t size);
+typedef void (*aboot_tiny_mem_free_t)(void *ptr);
+typedef int (*aboot_tiny_log_printf_t)(const char *format, ...);
+typedef int (*aboot_tiny_usleep_t)(unsigned long usec);
+
+/**
+ * Global function pointer should be assign by porting implementation
+ */
+extern aboot_tiny_uart_send_t aboot_tiny_uart_send;
+extern aboot_tiny_firmware_read_line_t aboot_tiny_firmware_read_line;
+extern aboot_tiny_firmware_read_data_t aboot_tiny_firmware_read_data;
+extern aboot_tiny_clock_get_t aboot_tiny_clock_get;
+extern aboot_tiny_mem_alloc_t aboot_tiny_mem_alloc;
+extern aboot_tiny_mem_free_t aboot_tiny_mem_free;
+extern aboot_tiny_log_printf_t aboot_tiny_log_printf;
+extern aboot_tiny_usleep_t aboot_tiny_usleep;
+
+#ifndef WITH_CONTIKI
+
+/**
+ * System main loop needed for non-protothread environment
+ *
+ * @return 0 on success, otherwise failed
+ */
+int ABOOT_API aboot_tiny_main_loop(void);
+
+#ifdef __linux__
+struct select_callback {
+ int (*set_fd)(fd_set *fdr, fd_set *fdw);
+ void (*handle_fd)(fd_set *fdr, fd_set *fdw);
+};
+int aboot_tiny_select_set_callback(int fd, const struct select_callback *callback);
+#endif
+
+#endif /* WITH_CONTIKI */
+
+/**
+ * Initialize base platform environment
+ *
+ * @return 0 on success, otherwise failed
+ */
+int ABOOT_API aboot_tiny_platform_init(void);
+
+/**
+ * Callback for uart rx data ready.
+ *
+ * @param data uart rx data pointer
+ * @param size uart rx data size
+ *
+ * @return data size processed
+ *
+ */
+size_t ABOOT_API aboot_tiny_uart_rx_callback(const uint8_t *data, size_t size);
+
+/**
+ * Get aboot max download size.
+ *
+ * @return max download size
+ *
+ */
+size_t ABOOT_API aboot_tiny_get_max_download_size(void);
+
+/**
+ * Set aboot link lost check timeout.
+ *
+ * @param second link lost check timeout in seconds
+ *
+ */
+void ABOOT_API aboot_tiny_set_link_lost_timeout(int second);
+
+/**
+ * Set aboot command response check timeout.
+ *
+ * @param second command response check timeout in seconds
+ *
+ */
+void ABOOT_API aboot_tiny_set_cmd_response_timeout(int second);
+
+/**
+ * Initialize aboot tiny engine.
+ *
+ * @param cb callback function pointer
+ *
+ * @return 0 on success, otherwise failed
+ */
+int ABOOT_API aboot_tiny_init(aboot_tiny_callback_t cb);
+
+/**
+ * Start aboot download engine.
+ *
+ * @param progress_fixup downloading extra image progress
+ * @param reboot if reboot after completed
+ *
+ * @return 0 on success, otherwise failed
+ */
+int ABOOT_API aboot_tiny_start(firmware_handle_t *firmware,
+ size_t progress_fixup, bool reboot);
+
+/**
+ * Begin aboot download process
+ *
+ * @return 0 on success, otherwise failed
+ */
+int ABOOT_API aboot_tiny_download(void);
+
+/**
+ * Stop aboot download engine.
+ *
+ * @return 0 on success, otherwise failed
+ */
+int ABOOT_API aboot_tiny_stop(void);
+
+/**
+ * Cleanup aboot tiny engine.
+ */
+int ABOOT_API aboot_tiny_exit(void);
+
+/**
+ * Check if aboot tiny engine is running
+ *
+ * @return true for running, otherwise false
+ */
+bool aboot_tiny_is_running(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ABOOT_TINY_H */
diff --git a/mbtk/aboot-tiny/include/sparse_defs.h b/mbtk/aboot-tiny/include/sparse_defs.h
new file mode 100755
index 0000000..b99cfd5
--- /dev/null
+++ b/mbtk/aboot-tiny/include/sparse_defs.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBSPARSE_SPARSE_DEFS_
+#define _LIBSPARSE_SPARSE_DEFS_
+
+#include <errno.h>
+#include <stdio.h>
+
+#define __le64 u64
+#define __le32 u32
+#define __le16 u16
+
+#define __be64 u64
+#define __be32 u32
+#define __be16 u16
+
+#define __u64 u64
+#define __u32 u32
+#define __u16 u16
+#define __u8 u8
+
+typedef unsigned long long u64;
+typedef signed long long s64;
+typedef unsigned int u32;
+typedef unsigned short int u16;
+typedef unsigned char u8;
+
+#define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
+#define ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
+#define ALIGN_DOWN(x, y) ((y) * ((x) / (y)))
+
+#define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); } while (0)
+#define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
+
+#endif
diff --git a/mbtk/aboot-tiny/include/sparse_format.h b/mbtk/aboot-tiny/include/sparse_format.h
new file mode 100755
index 0000000..779e038
--- /dev/null
+++ b/mbtk/aboot-tiny/include/sparse_format.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBSPARSE_SPARSE_FORMAT_H_
+#define _LIBSPARSE_SPARSE_FORMAT_H_
+#include "sparse_defs.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct sparse_header {
+ __le32 magic; /* 0xed26ff3a */
+ __le16 major_version; /* (0x1) - reject images with higher major versions */
+ __le16 minor_version; /* (0x0) - allow images with higer minor versions */
+ __le16 file_hdr_sz; /* 28 bytes for first revision of the file format */
+ __le16 chunk_hdr_sz; /* 12 bytes for first revision of the file format */
+ __le32 blk_sz; /* block size in bytes, must be a multiple of 4 (4096) */
+ __le32 total_blks; /* total blocks in the non-sparse output image */
+ __le32 total_chunks; /* total chunks in the sparse input image */
+ __le32 image_checksum; /* CRC32 checksum of the original data, counting "don't care" */
+ /* as 0. Standard 802.3 polynomial, use a Public Domain */
+ /* table implementation */
+} sparse_header_t;
+
+#define SPARSE_HEADER_MAGIC 0xed26ff3a
+
+#define CHUNK_TYPE_RAW 0xCAC1
+#define CHUNK_TYPE_FILL 0xCAC2
+#define CHUNK_TYPE_DONT_CARE 0xCAC3
+#define CHUNK_TYPE_CRC32 0xCAC4
+
+typedef struct chunk_header {
+ __le16 chunk_type; /* 0xCAC1 -> raw; 0xCAC2 -> fill; 0xCAC3 -> don't care */
+ __le16 reserved1;
+ __le32 chunk_sz; /* in blocks in output image */
+ __le32 total_sz; /* in bytes of chunk input file including chunk header and data */
+} chunk_header_t;
+
+/* Following a Raw or Fill or CRC32 chunk is data.
+ * For a Raw chunk, it's the data in chunk_sz * blk_sz.
+ * For a Fill chunk, it's 4 bytes of the fill data.
+ * For a CRC32 chunk, it's 4 bytes of CRC32
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif