t

Dependencies:   DM_FATFileSystem DM_HttpServer DM_USBHost EthernetInterface USBDevice mbed-rpc mbed-rtos

Fork of DMSupport by Embedded Artists

Committer:
embeddedartists
Date:
Fri Jan 23 13:48:44 2015 +0100
Revision:
27:0499c29688cc
Parent:
26:a65fbb4bde5c
Child:
28:8ae20cb0b943
- Temporarily added the mbed_mac_address call to the BiosLoader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 22:1a58a518435c 1 /*
embeddedartists 22:1a58a518435c 2 * Copyright 2014 Embedded Artists AB
embeddedartists 22:1a58a518435c 3 *
embeddedartists 22:1a58a518435c 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 22:1a58a518435c 5 * you may not use this file except in compliance with the License.
embeddedartists 22:1a58a518435c 6 * You may obtain a copy of the License at
embeddedartists 22:1a58a518435c 7 *
embeddedartists 22:1a58a518435c 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 22:1a58a518435c 9 *
embeddedartists 22:1a58a518435c 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 22:1a58a518435c 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 22:1a58a518435c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 22:1a58a518435c 13 * See the License for the specific language governing permissions and
embeddedartists 22:1a58a518435c 14 * limitations under the License.
embeddedartists 22:1a58a518435c 15 */
embeddedartists 22:1a58a518435c 16
embeddedartists 22:1a58a518435c 17 #include "mbed.h"
embeddedartists 22:1a58a518435c 18 #include "BiosLoader.h"
embeddedartists 22:1a58a518435c 19 #include "DMBoard.h"
embeddedartists 22:1a58a518435c 20 #include "BiosEEPROM.h"
embeddedartists 22:1a58a518435c 21 #include "crc.h"
embeddedartists 22:1a58a518435c 22 #include "bios.h"
embeddedartists 22:1a58a518435c 23
embeddedartists 22:1a58a518435c 24 #if defined(DM_BOARD_BIOS_DEVELOPMENT)
embeddedartists 22:1a58a518435c 25 #ifdef __cplusplus
embeddedartists 22:1a58a518435c 26 extern "C" {
embeddedartists 22:1a58a518435c 27 #endif
embeddedartists 26:a65fbb4bde5c 28 void bios_debug_aid(bios_header_t* header, const char** pMsg, uint32_t* paramSize);
embeddedartists 22:1a58a518435c 29 #ifdef __cplusplus
embeddedartists 22:1a58a518435c 30 }
embeddedartists 22:1a58a518435c 31 #endif
embeddedartists 22:1a58a518435c 32 #endif
embeddedartists 22:1a58a518435c 33
embeddedartists 22:1a58a518435c 34 /******************************************************************************
embeddedartists 22:1a58a518435c 35 * Defines and typedefs
embeddedartists 22:1a58a518435c 36 *****************************************************************************/
embeddedartists 22:1a58a518435c 37
embeddedartists 22:1a58a518435c 38 #define MOVE_POINTER(__x, __off) ( ( (uint32_t*)(__x) ) = (uint32_t*)( (uint32_t)(__x) + (__off) ) )
embeddedartists 22:1a58a518435c 39
embeddedartists 22:1a58a518435c 40 /*
embeddedartists 22:1a58a518435c 41 * Make sure that we reserve at least this amount of RAM for future
embeddedartists 22:1a58a518435c 42 * expansion of the BIOS. This prevents the user from squeezing out
embeddedartists 22:1a58a518435c 43 * the last drop of available RAM in his application.
embeddedartists 22:1a58a518435c 44 */
embeddedartists 22:1a58a518435c 45 #define BIOS_RESERVED_CHUNK 0x1000
embeddedartists 22:1a58a518435c 46 #define BIOS_MAX_SIZE 0x100000
embeddedartists 22:1a58a518435c 47 #ifndef MAX
embeddedartists 22:1a58a518435c 48 #define MAX(__a, __b) (((__a)>(__b))?(__a):(__b))
embeddedartists 22:1a58a518435c 49 #endif
embeddedartists 22:1a58a518435c 50
embeddedartists 26:a65fbb4bde5c 51 /*
embeddedartists 26:a65fbb4bde5c 52 * The BIOS is API compatible as long as the Major component of the
embeddedartists 26:a65fbb4bde5c 53 * version is the same.
embeddedartists 26:a65fbb4bde5c 54 */
embeddedartists 26:a65fbb4bde5c 55 #define SUPPORTED_BIOS_VER 0x000000
embeddedartists 26:a65fbb4bde5c 56 #define SUPPORTED_BIOS_MASK 0xff0000 // only look at the Major component
embeddedartists 26:a65fbb4bde5c 57 #define SUPPORTED_VERSION(__ver) (((__ver)&SUPPORTED_BIOS_MASK) == SUPPORTED_BIOS_VER)
embeddedartists 26:a65fbb4bde5c 58
embeddedartists 22:1a58a518435c 59 /******************************************************************************
embeddedartists 22:1a58a518435c 60 * Local variables
embeddedartists 22:1a58a518435c 61 *****************************************************************************/
embeddedartists 22:1a58a518435c 62
embeddedartists 22:1a58a518435c 63 /******************************************************************************
embeddedartists 22:1a58a518435c 64 * Private Functions
embeddedartists 22:1a58a518435c 65 *****************************************************************************/
embeddedartists 22:1a58a518435c 66
embeddedartists 22:1a58a518435c 67 // Function called from the BIOS
embeddedartists 22:1a58a518435c 68 static uint32_t readTimeMs()
embeddedartists 22:1a58a518435c 69 {
embeddedartists 22:1a58a518435c 70 return us_ticker_read()/1000;
embeddedartists 22:1a58a518435c 71 }
embeddedartists 22:1a58a518435c 72
embeddedartists 22:1a58a518435c 73
embeddedartists 22:1a58a518435c 74 BiosLoader::BiosLoader() :
embeddedartists 22:1a58a518435c 75 _initialized(false),
embeddedartists 22:1a58a518435c 76 _biosData(NULL),
embeddedartists 22:1a58a518435c 77 _conf(NULL),
embeddedartists 22:1a58a518435c 78 _confSize(0)
embeddedartists 22:1a58a518435c 79 {
embeddedartists 22:1a58a518435c 80 }
embeddedartists 22:1a58a518435c 81
embeddedartists 22:1a58a518435c 82 BiosLoader::~BiosLoader()
embeddedartists 22:1a58a518435c 83 {
embeddedartists 22:1a58a518435c 84 if (_biosData != NULL) {
embeddedartists 22:1a58a518435c 85 free(_biosData);
embeddedartists 22:1a58a518435c 86 _biosData = NULL;
embeddedartists 22:1a58a518435c 87 }
embeddedartists 22:1a58a518435c 88 if (_conf != NULL) {
embeddedartists 22:1a58a518435c 89 free(_conf);
embeddedartists 22:1a58a518435c 90 _conf = NULL;
embeddedartists 22:1a58a518435c 91 _confSize = 0;
embeddedartists 22:1a58a518435c 92 }
embeddedartists 22:1a58a518435c 93 }
embeddedartists 22:1a58a518435c 94
embeddedartists 22:1a58a518435c 95 DMBoard::BoardError BiosLoader::readBIOS(uint8_t** data, uint32_t* size)
embeddedartists 22:1a58a518435c 96 {
embeddedartists 22:1a58a518435c 97 DMBoard::BoardError err = DMBoard::Ok;
embeddedartists 22:1a58a518435c 98 BiosEEPROM eeprom;
embeddedartists 22:1a58a518435c 99 file_header_t fh;
embeddedartists 22:1a58a518435c 100
embeddedartists 22:1a58a518435c 101 if (_conf != NULL) {
embeddedartists 22:1a58a518435c 102 *data = _conf;
embeddedartists 22:1a58a518435c 103 *size = _confSize;
embeddedartists 22:1a58a518435c 104 return DMBoard::Ok;
embeddedartists 22:1a58a518435c 105 }
embeddedartists 22:1a58a518435c 106
embeddedartists 22:1a58a518435c 107 do {
embeddedartists 22:1a58a518435c 108 if (!eeprom.read(0, (char*)&fh, sizeof(file_header_t))) {
embeddedartists 22:1a58a518435c 109 resetI2C();
embeddedartists 22:1a58a518435c 110 if (!eeprom.read(0, (char*)&fh, sizeof(file_header_t))) {
embeddedartists 22:1a58a518435c 111 err = DMBoard::BiosStorageError;
embeddedartists 22:1a58a518435c 112 break;
embeddedartists 22:1a58a518435c 113 }
embeddedartists 22:1a58a518435c 114 }
embeddedartists 22:1a58a518435c 115
embeddedartists 22:1a58a518435c 116 if (fh.magic != BIOS_MAGIC) {
embeddedartists 22:1a58a518435c 117 err = DMBoard::BiosInvalidError;
embeddedartists 22:1a58a518435c 118 break;
embeddedartists 22:1a58a518435c 119 }
embeddedartists 22:1a58a518435c 120
embeddedartists 26:a65fbb4bde5c 121 if (!SUPPORTED_VERSION(fh.version)) {
embeddedartists 22:1a58a518435c 122 err = DMBoard::BiosVersionError;
embeddedartists 22:1a58a518435c 123 break;
embeddedartists 22:1a58a518435c 124 }
embeddedartists 22:1a58a518435c 125
embeddedartists 22:1a58a518435c 126 if ((fh.headerSize + fh.size) > BIOS_MAX_SIZE) {
embeddedartists 22:1a58a518435c 127 err = DMBoard::BiosInvalidError;
embeddedartists 22:1a58a518435c 128 break;
embeddedartists 22:1a58a518435c 129 }
embeddedartists 22:1a58a518435c 130
embeddedartists 22:1a58a518435c 131 _confSize = fh.headerSize + fh.size;
embeddedartists 22:1a58a518435c 132 _conf = (uint8_t*)malloc(MAX(_confSize,BIOS_RESERVED_CHUNK));
embeddedartists 22:1a58a518435c 133 if (_conf == NULL) {
embeddedartists 22:1a58a518435c 134 _confSize = 0;
embeddedartists 22:1a58a518435c 135 err = DMBoard::MemoryError;
embeddedartists 22:1a58a518435c 136 break;
embeddedartists 22:1a58a518435c 137 }
embeddedartists 22:1a58a518435c 138
embeddedartists 22:1a58a518435c 139 if (!eeprom.read(0, (char*)_conf, _confSize)) {
embeddedartists 22:1a58a518435c 140 err = DMBoard::BiosStorageError;
embeddedartists 22:1a58a518435c 141 break;
embeddedartists 22:1a58a518435c 142 }
embeddedartists 22:1a58a518435c 143
embeddedartists 22:1a58a518435c 144 uint32_t crc = crc_Buffer((uint32_t*)(&_conf[fh.headerSize]), fh.size/4);
embeddedartists 22:1a58a518435c 145 if (crc != fh.crc) {
embeddedartists 22:1a58a518435c 146 err = DMBoard::BiosInvalidError;
embeddedartists 22:1a58a518435c 147 break;
embeddedartists 22:1a58a518435c 148 }
embeddedartists 22:1a58a518435c 149
embeddedartists 22:1a58a518435c 150 // Bios header has been verified and seems ok
embeddedartists 22:1a58a518435c 151 *data = _conf;
embeddedartists 22:1a58a518435c 152 *size = _confSize;
embeddedartists 22:1a58a518435c 153 err = DMBoard::Ok;
embeddedartists 22:1a58a518435c 154 } while (false);
embeddedartists 22:1a58a518435c 155
embeddedartists 22:1a58a518435c 156 if (err != DMBoard::Ok) {
embeddedartists 22:1a58a518435c 157 if (_conf != NULL) {
embeddedartists 22:1a58a518435c 158 free(_conf);
embeddedartists 22:1a58a518435c 159 _conf = NULL;
embeddedartists 22:1a58a518435c 160 _confSize = 0;
embeddedartists 22:1a58a518435c 161 }
embeddedartists 22:1a58a518435c 162 }
embeddedartists 22:1a58a518435c 163
embeddedartists 22:1a58a518435c 164 return err;
embeddedartists 22:1a58a518435c 165 }
embeddedartists 22:1a58a518435c 166
embeddedartists 22:1a58a518435c 167 DMBoard::BoardError BiosLoader::params(bios_header_t** header, void** instanceData)
embeddedartists 22:1a58a518435c 168 {
embeddedartists 22:1a58a518435c 169 if (!_initialized) {
embeddedartists 22:1a58a518435c 170 DMBoard::BoardError err = init();
embeddedartists 22:1a58a518435c 171 if (err != DMBoard::Ok) {
embeddedartists 22:1a58a518435c 172 return err;
embeddedartists 22:1a58a518435c 173 }
embeddedartists 22:1a58a518435c 174 }
embeddedartists 22:1a58a518435c 175 if (_initialized) {
embeddedartists 22:1a58a518435c 176 *header = &_bios;
embeddedartists 22:1a58a518435c 177 *instanceData = _biosData;
embeddedartists 22:1a58a518435c 178 return DMBoard::Ok;
embeddedartists 22:1a58a518435c 179 } else {
embeddedartists 22:1a58a518435c 180 return DMBoard::BiosInvalidError;
embeddedartists 22:1a58a518435c 181 }
embeddedartists 22:1a58a518435c 182 }
embeddedartists 22:1a58a518435c 183
embeddedartists 22:1a58a518435c 184 DMBoard::BoardError BiosLoader::init()
embeddedartists 22:1a58a518435c 185 {
embeddedartists 22:1a58a518435c 186 DMBoard::BoardError err = DMBoard::Ok;
embeddedartists 22:1a58a518435c 187 if (!_initialized) {
embeddedartists 22:1a58a518435c 188 do {
embeddedartists 22:1a58a518435c 189
embeddedartists 22:1a58a518435c 190 // Get the display bios from the DMBoard. DMBoard will have verified it
embeddedartists 22:1a58a518435c 191 // and will keep it in RAM so there is no need to copy it.
embeddedartists 22:1a58a518435c 192 uint8_t* p = NULL;
embeddedartists 22:1a58a518435c 193 uint32_t size = 0;
embeddedartists 22:1a58a518435c 194 err = readBIOS(&p, &size);
embeddedartists 22:1a58a518435c 195 if (err != BiosError_Ok) {
embeddedartists 22:1a58a518435c 196 break;
embeddedartists 22:1a58a518435c 197 }
embeddedartists 22:1a58a518435c 198
embeddedartists 27:0499c29688cc 199 // The BIOS has been read so we know that the I2C bus is working. After the
embeddedartists 27:0499c29688cc 200 // BIOS is "started" it will take ownership of the bus and it can cause
embeddedartists 27:0499c29688cc 201 // problems for other peripherals on it. The only other peripheral today
embeddedartists 27:0499c29688cc 202 // is the EEPROM with the MAC address. It is read by mbed_mac_address() in
embeddedartists 27:0499c29688cc 203 // ethernet_api.c in the SDK and it will be cached. By reading it here now
embeddedartists 27:0499c29688cc 204 // we prevent future access to the I2C bus.
embeddedartists 27:0499c29688cc 205 char mac[6] = {0};
embeddedartists 27:0499c29688cc 206 mbed_mac_address(mac);
embeddedartists 27:0499c29688cc 207
embeddedartists 22:1a58a518435c 208 // Extract the function pointers so that they can be modified to match the
embeddedartists 22:1a58a518435c 209 // actual location of the code
embeddedartists 22:1a58a518435c 210 file_header_t* file_header = (file_header_t*)p;
embeddedartists 22:1a58a518435c 211 memcpy(&_bios, &file_header->header, sizeof(bios_header_t));
embeddedartists 22:1a58a518435c 212
embeddedartists 22:1a58a518435c 213 // Allocate memory for the BIOS instance data
embeddedartists 22:1a58a518435c 214 _biosData = malloc(file_header->paramSize);
embeddedartists 22:1a58a518435c 215 if (_biosData == NULL) {
embeddedartists 22:1a58a518435c 216 err = DMBoard::MemoryError;
embeddedartists 22:1a58a518435c 217 break;
embeddedartists 22:1a58a518435c 218 }
embeddedartists 22:1a58a518435c 219
embeddedartists 22:1a58a518435c 220 // All offsets must be moved by two factors:
embeddedartists 22:1a58a518435c 221 // 1) The position of the code in RAM (location of "p")
embeddedartists 22:1a58a518435c 222 // 2) The header size (the code/data comes after it)
embeddedartists 22:1a58a518435c 223 uint32_t offset = ((uint32_t)p) + file_header->headerSize;
embeddedartists 22:1a58a518435c 224 uint32_t* functions = (uint32_t*)&_bios;
embeddedartists 22:1a58a518435c 225 for (int i = 0; i < (sizeof(bios_header_t)/sizeof(uint32_t)); i++) {
embeddedartists 22:1a58a518435c 226 functions[i] += offset;
embeddedartists 22:1a58a518435c 227 }
embeddedartists 22:1a58a518435c 228
embeddedartists 22:1a58a518435c 229 #if defined(DM_BOARD_BIOS_DEVELOPMENT)
embeddedartists 22:1a58a518435c 230 // This requires that the project contains the source code for the BIOS
embeddedartists 24:9a677afc86f1 231 const char* msg;
embeddedartists 26:a65fbb4bde5c 232 uint32_t tmp = 0;
embeddedartists 26:a65fbb4bde5c 233 bios_debug_aid(&_bios, &msg, &tmp);
embeddedartists 26:a65fbb4bde5c 234 if (tmp > file_header->paramSize) {
embeddedartists 26:a65fbb4bde5c 235 free(_biosData);
embeddedartists 26:a65fbb4bde5c 236 _biosData = malloc(tmp);
embeddedartists 26:a65fbb4bde5c 237 if (_biosData == NULL) {
embeddedartists 26:a65fbb4bde5c 238 err = DMBoard::MemoryError;
embeddedartists 26:a65fbb4bde5c 239 break;
embeddedartists 26:a65fbb4bde5c 240 }
embeddedartists 26:a65fbb4bde5c 241 }
embeddedartists 24:9a677afc86f1 242 DMBoard::instance().logger()->printf("BIOS info: %s\n", msg);
embeddedartists 22:1a58a518435c 243 #endif
embeddedartists 22:1a58a518435c 244
embeddedartists 22:1a58a518435c 245 // Prepare the BIOS instance data before calling the first function
embeddedartists 22:1a58a518435c 246 BiosError_t e = _bios.initParams(_biosData, SystemCoreClock, PeripheralClock, wait_us, readTimeMs);
embeddedartists 22:1a58a518435c 247 if (e != BiosError_Ok) {
embeddedartists 22:1a58a518435c 248 err = DMBoard::BiosInvalidError;
embeddedartists 22:1a58a518435c 249 break;
embeddedartists 22:1a58a518435c 250 }
embeddedartists 22:1a58a518435c 251
embeddedartists 22:1a58a518435c 252 _initialized = true;
embeddedartists 22:1a58a518435c 253 } while(0);
embeddedartists 22:1a58a518435c 254 }
embeddedartists 22:1a58a518435c 255 return err;
embeddedartists 22:1a58a518435c 256 }
embeddedartists 22:1a58a518435c 257
embeddedartists 22:1a58a518435c 258 void BiosLoader::resetI2C()
embeddedartists 22:1a58a518435c 259 {
embeddedartists 22:1a58a518435c 260 DMBoard::instance().logger()->printf("BiosLoader::resetI2C()\n");
embeddedartists 22:1a58a518435c 261 DigitalOut reset(P0_23);
embeddedartists 22:1a58a518435c 262 reset = 0;
embeddedartists 22:1a58a518435c 263 wait_ms(1);
embeddedartists 22:1a58a518435c 264 reset = 1;
embeddedartists 22:1a58a518435c 265 wait_ms(10);
embeddedartists 22:1a58a518435c 266 }
embeddedartists 22:1a58a518435c 267
embeddedartists 22:1a58a518435c 268
embeddedartists 22:1a58a518435c 269 /******************************************************************************
embeddedartists 22:1a58a518435c 270 * Public Functions
embeddedartists 22:1a58a518435c 271 *****************************************************************************/
embeddedartists 22:1a58a518435c 272
embeddedartists 22:1a58a518435c 273 bool BiosLoader::isKnownSPIFIMemory(uint8_t mfgr, uint8_t devType, uint8_t devID, uint32_t memSize, uint32_t* eraseBlockSize)
embeddedartists 22:1a58a518435c 274 {
embeddedartists 22:1a58a518435c 275 if (!_initialized) {
embeddedartists 22:1a58a518435c 276 DMBoard::BoardError err = init();
embeddedartists 22:1a58a518435c 277 if (err != DMBoard::Ok) {
embeddedartists 22:1a58a518435c 278 return false;
embeddedartists 22:1a58a518435c 279 }
embeddedartists 22:1a58a518435c 280 }
embeddedartists 22:1a58a518435c 281 if (_initialized) {
embeddedartists 22:1a58a518435c 282 bool known = false;
embeddedartists 22:1a58a518435c 283 BiosError_t err = _bios.spifiIsSupported(_biosData, mfgr,devType,devID,memSize,&known,eraseBlockSize);
embeddedartists 22:1a58a518435c 284 if (err == BiosError_Ok) {
embeddedartists 22:1a58a518435c 285 return known;
embeddedartists 22:1a58a518435c 286 }
embeddedartists 22:1a58a518435c 287 }
embeddedartists 22:1a58a518435c 288 return false;
embeddedartists 22:1a58a518435c 289 }