SimpleLink CC32xx OTA Library
Simplifies the implementation of Internet connectivity
OtaArchive.h
1 /*
2  * Copyright (c) 2018, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #ifndef _OTA_ARCHIVE_H__
33 #define _OTA_ARCHIVE_H__
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #include <ti/drivers/crypto/CryptoCC32XX.h>
40 
41 #define OTA_ARCHIVE_VERSION "OTA_ARCHIVE_2.0.0.4"
42 
43 /* RunStatus */
44 #define ARCHIVE_STATUS_FORCE_READ_MORE (2L)
45 #define ARCHIVE_STATUS_DOWNLOAD_DONE (1L)
46 #define ARCHIVE_STATUS_CONTINUE (0L)
47 #define ARCHIVE_STATUS_OK (0L)
48 #define ARCHIVE_STATUS_ERROR_STATE (-20101L)
49 #define ARCHIVE_STATUS_ERROR_FILE_NAME_SLASHES (-20102L)
50 #define ARCHIVE_STATUS_ERROR_BUNDLE_CMD_FILE_NAME (-20103L)
51 #define ARCHIVE_STATUS_ERROR_BUNDLE_CMD_SKIP_OBJECT (-20104L)
52 #define ARCHIVE_STATUS_ERROR_BUNDLE_CMD_ERROR (-20105L)
53 #define ARCHIVE_STATUS_ERROR_OPEN_FILE (-20106L)
54 #define ARCHIVE_STATUS_ERROR_SAVE_CHUNK (-20107L)
55 #define ARCHIVE_STATUS_ERROR_CLOSE_FILE (-20108L)
56 #define ARCHIVE_STATUS_ERROR_BUNDLE_CMD_MAX_OBJECT (-20109L)
57 #define ARCHIVE_STATUS_ERROR_SECURITY_ALERT (-20199L)
58 
59 typedef enum
60 {
61  ARCHIVE_STATE_IDLE = 0,
62  ARCHIVE_STATE_PARSE_HDR,
63  ARCHIVE_STATE_PARSE_CMD_FILE,
64  ARCHIVE_STATE_PARSE_CMD_SIGNATURE_FILE,
65  ARCHIVE_STATE_OPEN_FILE,
66  ARCHIVE_STATE_SAVE_FILE,
67  ARCHIVE_STATE_COMPLETE_PENDING_TESTING,
68  ARCHIVE_STATE_PARSING_FAILED
69 } OtaArchiveState_e;
70 
71 #define TAR_HDR_SIZE 512
72 #define MAX_SIGNATURE_SIZE 256
73 #define MAX_FILE_NAME_SIZE 128
74 #define MAX_SHA256_DIGEST_SIZE 65
75 #define MAX_BUNDLE_CMD_FILES 8
76 
77 #ifdef SL_OTA_ARCHIVE_STANDALONE
78 #define VERSION_STR_SIZE 14 /* sizeof "YYYYMMDDHHMMSS" */
79 extern int ltoa(long val, char *buffer);
80 extern int Report(const char *format, ...);
81 #define _SlOtaLibTrace(pargs) Report pargs
82 #endif
83 
84 typedef struct
85 {
86  uint8_t FileNameBuf[MAX_FILE_NAME_SIZE];
87  uint8_t CertificateFileNameBuf[MAX_FILE_NAME_SIZE];
88  uint8_t SignatureBuf[MAX_SIGNATURE_SIZE];
89  uint32_t SignatureLen;
90  uint8_t Sha256Digest[MAX_SHA256_DIGEST_SIZE];
91  uint16_t Sha256DigestLen;
92  uint32_t Secured;
93  uint32_t Bundle;
94  uint8_t SavedInFS;
96 
97 typedef struct
98 {
99  int16_t NumFiles;
100  int16_t NumFilesSavedInFS;
101  OtaArchive_BundleFileInfo_t BundleFileInfo[MAX_BUNDLE_CMD_FILES];
102  uint8_t VerifiedSignature;
103  uint16_t TotalParsedBytes;
105 
106 typedef struct
107 {
108  /* File info from TAR file header */
109  uint8_t FileNameBuf[MAX_FILE_NAME_SIZE];
110  uint8_t *pFileName;
111  uint32_t FileSize;
112  int16_t FileType;
113 
114  uint32_t ulToken;
115  int32_t lFileHandle;
116  uint32_t WriteFileOffset;
117 } TarObj_t;
118 
119 typedef struct
120 {
121  char VersionFilename[VERSION_STR_SIZE+1];
123 
124 typedef struct
125 {
126  OtaArchiveState_e State; /* internal archive state machine state */
127  int32_t TotalBytesReceived; /* Should be keeped over states */
128  TarObj_t CurrTarObj; /* Current file info from the TAR file itself */
129  OtaArchive_BundleCmdTable_t BundleCmdTable; /* Table of files info from "ota.cmd" */
130  int32_t SavingStarted; /* if 1 on error need rollback */
131  OtaVersionFile_t OtaVersionFile; /* save version file to save on download done */
132 } OtaArchive_t;
133 
134 /*
135  get a chunk of the the tar file and process that
136 */
137 int16_t OtaArchive_init(OtaArchive_t *pOtaArchive);
138 int16_t OtaArchive_process(OtaArchive_t *pOtaArchive, uint8_t *pBuf, int16_t BufLen, int16_t *pProcessedBytes);
139 int16_t OtaArchive_abort(OtaArchive_t *pOtaArchive);
140 int16_t OtaArchive_getStatus(OtaArchive_t *pOtaArchive);
141 int16_t OtaArchive_rollback(void);
142 int16_t OtaArchive_commit(void);
143 int16_t OtaArchive_getPendingCommit(void);
144 int16_t OtaArchive_checkVersion(OtaArchive_t *pOtaArchive, uint8_t *pFileName);
145 int16_t OtaArchive_getCurrentVersion(uint8_t *pVersionBuf);
146 
147 #ifdef __cplusplus
148 }
149 #endif /* __cplusplus */
150 
151 #endif /* _OTA_ARCHIVE_H__ */