1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _BOOTLOADER_H_
18 #define _BOOTLOADER_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include <stdbool.h>
25 #include <stdarg.h>
26 #include <stdint.h>
27 
28 struct Sha2state;
29 struct RsaState;
30 struct AesContext;
31 struct AesSetupTempWorksSpace;
32 struct AesCbcContext;
33 
34 #define OS_UPDT_SUCCESS                0
35 #define OS_UPDT_HDR_CHECK_FAILED       1
36 #define OS_UPDT_HDR_MARKER_INVALID     2
37 #define OS_UPDT_UNKNOWN_PUBKEY         3
38 #define OS_UPDT_INVALID_SIGNATURE      4
39 #define OS_UPDT_INVALID_SIGNATURE_HASH 5
40 
41 #define BL_SCAN_OFFSET      0x00000100
42 
43 #define BL_VERSION_1        1
44 #define BL_VERSION_CUR      BL_VERSION_1
45 
46 #define BL_FLASH_KEY1       0x45670123
47 #define BL_FLASH_KEY2       0xCDEF89AB
48 
49 struct BlVecTable {
50     /* cortex requirements */
51     uint32_t    blStackTop;
52     void        (*blEntry)(void);
53     void        (*blNmiHandler)(void);
54     void        (*blHardFaultHandler)(void);
55     void        (*blMmuFaultHandler)(void);
56     void        (*blBusFaultHandler)(void);
57     void        (*blUsageFaultHandler)(void);
58 
59     /* bl api */
60 
61     //ver 1 bl supports:
62 
63     //basics
64     uint32_t        (*blGetVersion)(void);
65     void            (*blReboot)(void);
66     void            (*blGetSnum)(uint32_t *snum, uint32_t length);
67 
68     //flash
69     bool            (*blProgramShared)(uint8_t *dst, const uint8_t *src, uint32_t length, uint32_t key1, uint32_t key2);
70     bool            (*blEraseShared)(uint32_t key1, uint32_t key2);
71     bool            (*blProgramEe)(uint8_t *dst, const uint8_t *src, uint32_t length, uint32_t key1, uint32_t key2);
72 
73     //security data
74     const uint32_t* (*blGetPubKeysInfo)(uint32_t *numKeys);
75 
76     //hashing, encryption, signature apis
77     const uint32_t* (*blRsaPubOpIterative)(struct RsaState* state, const uint32_t *a, const uint32_t *c, uint32_t *state1, uint32_t *state2, uint32_t *stepP);
78     void            (*blSha2init)(struct Sha2state *state);
79     void            (*blSha2processBytes)(struct Sha2state *state, const void *bytes, uint32_t numBytes);
80     const uint32_t* (*blSha2finish)(struct Sha2state *state);
81     void            (*blAesInitForEncr)(struct AesContext *ctx, const uint32_t *k);
82     void            (*blAesInitForDecr)(struct AesContext *ctx, struct AesSetupTempWorksSpace *tmpSpace, const uint32_t *k);
83     void            (*blAesEncr)(struct AesContext *ctx, const uint32_t *src, uint32_t *dst);
84     void            (*blAesDecr)(struct AesContext *ctx, const uint32_t *src, uint32_t *dst);
85     void            (*blAesCbcInitForEncr)(struct AesCbcContext *ctx, const uint32_t *k, const uint32_t *iv);
86     void            (*blAesCbcInitForDecr)(struct AesCbcContext *ctx, const uint32_t *k, const uint32_t *iv);
87     void            (*blAesCbcEncr)(struct AesCbcContext *ctx, const uint32_t *src, uint32_t *dst);
88     void            (*blAesCbcDecr)(struct AesCbcContext *ctx, const uint32_t *src, uint32_t *dst);
89     const uint32_t* (*blSigPaddingVerify)(const uint32_t *rsaResult); //return pointer to hash inside the rsaResult or NULL on error
90 
91     // extension: for binary compatibility, placed here
92     uint32_t        (*blVerifyOsUpdate)(void);
93 };
94 
95 #ifndef BL_STACK_SIZE
96 #define BL_STACK_SIZE 4096
97 #endif
98 
99 
100 //for using outside of bootloader
101 extern struct BlVecTable BL;
102 
103 
104 
105 //for code in bootloader to log things
106 void blLog(const char *str, ...);
107 
108 #ifdef __cplusplus
109 }
110 #endif
111 
112 #endif
113