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 #ifndef _BL_H_
17 #define _BL_H_
18 
19 #include <stdbool.h>
20 #include <stdarg.h>
21 #include <stdint.h>
22 
23 #include <plat/bl.h>
24 
25 #define OS_UPDT_SUCCESS                0
26 #define OS_UPDT_HDR_CHECK_FAILED       1
27 #define OS_UPDT_HDR_MARKER_INVALID     2
28 #define OS_UPDT_UNKNOWN_PUBKEY         3
29 #define OS_UPDT_INVALID_SIGNATURE      4
30 #define OS_UPDT_INVALID_SIGNATURE_HASH 5
31 
32 #define BL_SCAN_OFFSET      0x00000100
33 
34 #define BL_VERSION_1        1
35 #define BL_VERSION_CUR      BL_VERSION_1
36 
37 #define BL _BL.api
38 
39 struct Sha2state;
40 struct RsaState;
41 struct AesContext;
42 struct AesSetupTempWorksSpace;
43 struct AesCbcContext;
44 
45 struct BlApiTable {
46     //ver 1 bl supports:
47 
48     //basics
49     uint32_t        (*blGetVersion)(void);
50     void            (*blReboot)(void);
51     void            (*blGetSnum)(uint32_t *snum, uint32_t length);
52 
53     //flash
54     bool            (*blProgramShared)(uint8_t *dst, const uint8_t *src, uint32_t length, uint32_t key1, uint32_t key2);
55     bool            (*blEraseShared)(uint32_t key1, uint32_t key2);
56     bool            (*blProgramEe)(uint8_t *dst, const uint8_t *src, uint32_t length, uint32_t key1, uint32_t key2);
57 
58     //security data
59     const uint32_t* (*blGetPubKeysInfo)(uint32_t *numKeys);
60 
61     //hashing, encryption, signature apis
62     const uint32_t* (*blRsaPubOpIterative)(struct RsaState* state, const uint32_t *a, const uint32_t *c, uint32_t *state1, uint32_t *state2, uint32_t *stepP);
63     void            (*blSha2init)(struct Sha2state *state);
64     void            (*blSha2processBytes)(struct Sha2state *state, const void *bytes, uint32_t numBytes);
65     const uint32_t* (*blSha2finish)(struct Sha2state *state);
66     void            (*blAesInitForEncr)(struct AesContext *ctx, const uint32_t *k);
67     void            (*blAesInitForDecr)(struct AesContext *ctx, struct AesSetupTempWorksSpace *tmpSpace, const uint32_t *k);
68     void            (*blAesEncr)(struct AesContext *ctx, const uint32_t *src, uint32_t *dst);
69     void            (*blAesDecr)(struct AesContext *ctx, const uint32_t *src, uint32_t *dst);
70     void            (*blAesCbcInitForEncr)(struct AesCbcContext *ctx, const uint32_t *k, const uint32_t *iv);
71     void            (*blAesCbcInitForDecr)(struct AesCbcContext *ctx, const uint32_t *k, const uint32_t *iv);
72     void            (*blAesCbcEncr)(struct AesCbcContext *ctx, const uint32_t *src, uint32_t *dst);
73     void            (*blAesCbcDecr)(struct AesCbcContext *ctx, const uint32_t *src, uint32_t *dst);
74     const uint32_t* (*blSigPaddingVerify)(const uint32_t *rsaResult); //return pointer to hash inside the rsaResult or NULL on error
75 
76     // extension: for binary compatibility, placed here
77     uint32_t        (*blVerifyOsUpdate)(void);
78 };
79 
80 struct BlTable {
81     struct BlVecTable vec;
82     struct BlApiTable api;
83 };
84 
85 //for using outside of bootloader
86 extern struct BlTable _BL;
87 
88 //for code in bootloader to log things
89 void blLog(const char *str, ...);
90 
91 bool blEraseSectors(uint32_t sector_cnt, uint8_t *erase_mask, uint32_t key1, uint32_t key2);
92 bool blPlatProgramFlash(uint8_t *dst, const uint8_t *src, uint32_t length, uint32_t key1, uint32_t key2);
93 uint32_t blDisableInts(void);
94 void blRestoreInts(uint32_t state);
95 void blReboot(void);
96 
97 // return device serial number in array pointed to by snum
98 // return value is actual copied data size in bytes
99 uint32_t blGetSnum(uint32_t *snum, uint32_t length);
100 // early platform init; after return from this call blHostActive() and blConfigIo() may be called immediately
101 void blSetup();
102 // platform cleanup before leaving bootloader; restore configuration altered by blSetup
103 // on return from this call system state is similar enough to conditions seen after HW reset
104 // it is safe to re-run all the bootloader code sequence again
105 // it is also safe to start the custom code from flash
106 void blCleanup();
107 // returns true if host is requesting us to start bootloader
108 bool blHostActive();
109 // prepare data channel to exchange data with host
110 void blConfigIo();
111 // reads data stream from host until synccode is seen; returns true if received synccode, false otherwise
112 // must be called after blConfigIo()
113 bool blSyncWait(uint32_t syncCode);
114 // reset IO channel HW;
115 // makes controller ready for next data packet
116 // current packet is abandoned
117 void blResetRxData();
118 
119 // exchange 1 byte in both directions (SPI only)
120 uint8_t blSpiTxRxByte(uint32_t val);
121 
122 // this must be called from reset vector handler
123 // once data bss and stack are set properly
124 // this method executes boot protocol, and returns when it is done
125 // after calling method bootloader must jump to the OS boot address
126 void blMain(uint32_t bootAddr);
127 
128 #endif /* _BL_H_ */
129