Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DM_FATFileSystem DM_HttpServer DM_USBHost EthernetInterface USBDevice mbed-rpc mbed-rtos
Fork of DMSupport by
BiosLoader.cpp
00001 /* 00002 * Copyright 2014 Embedded Artists AB 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "BiosLoader.h" 00019 #include "DMBoard.h" 00020 #include "BiosEEPROM.h" 00021 #include "crc.h" 00022 #include "bios.h" 00023 #include "meas.h" 00024 00025 #if defined(DM_BOARD_BIOS_DEVELOPMENT) 00026 #ifdef __cplusplus 00027 extern "C" { 00028 #endif 00029 void bios_debug_aid(bios_header_t* header, const char** pMsg, uint32_t* paramSize); 00030 #ifdef __cplusplus 00031 } 00032 #endif 00033 #endif 00034 00035 /****************************************************************************** 00036 * Defines and typedefs 00037 *****************************************************************************/ 00038 00039 #define MOVE_POINTER(__x, __off) ( ( (uint32_t*)(__x) ) = (uint32_t*)( (uint32_t)(__x) + (__off) ) ) 00040 00041 /* 00042 * Make sure that we reserve at least this amount of RAM for future 00043 * expansion of the BIOS. This prevents the user from squeezing out 00044 * the last drop of available RAM in his application. 00045 */ 00046 #define BIOS_RESERVED_CHUNK 0x1000 00047 #define BIOS_MAX_SIZE 0x100000 00048 #ifndef MAX 00049 #define MAX(__a, __b) (((__a)>(__b))?(__a):(__b)) 00050 #endif 00051 00052 /* 00053 * The BIOS is API compatible as long as the Major component of the 00054 * version is the same. 00055 */ 00056 #define SUPPORTED_BIOS_VER 0x000000 00057 #define SUPPORTED_BIOS_MASK 0xff0000 // only look at the Major component 00058 #define SUPPORTED_VERSION(__ver) (((__ver)&SUPPORTED_BIOS_MASK) == SUPPORTED_BIOS_VER) 00059 00060 //#define MAC_IN_SDK 00061 00062 /****************************************************************************** 00063 * Local variables 00064 *****************************************************************************/ 00065 00066 /****************************************************************************** 00067 * Global functions 00068 *****************************************************************************/ 00069 00070 #if !defined(MAC_IN_SDK) 00071 /* The LPC4088QSB platform in the MBED SDK have defined the WEAK function 00072 * mbed_mac_address (ethernet_api.c) to read a unique MAC address from the 00073 * onboard EEPROM. 00074 * The LPC4088DM platform in the MBED SDK does not have the WEAK function 00075 * so it is implemented here instead. This version of the function will ask 00076 * the bios for a MAC address. 00077 */ 00078 void mbed_mac_address(char *mac) { 00079 static char cache[6]; 00080 static bool haveIt = false; 00081 if (!haveIt) { 00082 BiosLoader::instance().getMacAddress(cache); 00083 haveIt = true; 00084 } 00085 memcpy(mac, cache, 6); 00086 } 00087 #endif 00088 00089 /****************************************************************************** 00090 * Private Functions 00091 *****************************************************************************/ 00092 00093 // Called by the NVIC 00094 static void loader_i2c0_irq_handler() 00095 { 00096 BiosLoader::instance().handleI2CInterrupt(); 00097 } 00098 00099 00100 // Function called from the BIOS 00101 static uint32_t readTimeMs() 00102 { 00103 return us_ticker_read()/1000; 00104 } 00105 00106 00107 BiosLoader::BiosLoader() : 00108 _initialized(false), 00109 _biosData(NULL), 00110 _conf(NULL), 00111 _confSize(0), 00112 _stats(0) 00113 { 00114 } 00115 00116 BiosLoader::~BiosLoader() 00117 { 00118 if (_biosData != NULL) { 00119 free(_biosData); 00120 _biosData = NULL; 00121 } 00122 if (_conf != NULL) { 00123 free(_conf); 00124 _conf = NULL; 00125 _confSize = 0; 00126 } 00127 } 00128 00129 DMBoard::BoardError BiosLoader::readBIOS(uint8_t** data, uint32_t* size) 00130 { 00131 DMBoard::BoardError err = DMBoard::Ok; 00132 BiosEEPROM eeprom; 00133 file_header_t fh; 00134 00135 if (_conf != NULL) { 00136 *data = _conf; 00137 *size = _confSize; 00138 return DMBoard::Ok; 00139 } 00140 00141 do { 00142 if (!eeprom.read(0, (char*)&fh, sizeof(file_header_t))) { 00143 resetI2C(); 00144 if (!eeprom.read(0, (char*)&fh, sizeof(file_header_t))) { 00145 err = DMBoard::BiosStorageError; 00146 break; 00147 } 00148 } 00149 00150 if (fh.magic != BIOS_MAGIC) { 00151 err = DMBoard::BiosInvalidError; 00152 break; 00153 } 00154 00155 if (!SUPPORTED_VERSION(fh.version)) { 00156 err = DMBoard::BiosVersionError; 00157 break; 00158 } 00159 00160 if ((fh.headerSize + fh.size) > BIOS_MAX_SIZE) { 00161 err = DMBoard::BiosInvalidError; 00162 break; 00163 } 00164 00165 _confSize = fh.headerSize + fh.size; 00166 _conf = (uint8_t*)malloc(MAX(_confSize,BIOS_RESERVED_CHUNK)); 00167 if (_conf == NULL) { 00168 _confSize = 0; 00169 err = DMBoard::MemoryError; 00170 break; 00171 } 00172 00173 if (!eeprom.read(0, (char*)_conf, _confSize)) { 00174 err = DMBoard::BiosStorageError; 00175 break; 00176 } 00177 00178 uint32_t crc = crc_Buffer((uint32_t*)(&_conf[fh.headerSize]), fh.size/4); 00179 if (crc != fh.crc) { 00180 err = DMBoard::BiosInvalidError; 00181 break; 00182 } 00183 00184 // Bios header has been verified and seems ok 00185 *data = _conf; 00186 *size = _confSize; 00187 _stats = fh.version; 00188 err = DMBoard::Ok; 00189 } while (false); 00190 00191 if (err != DMBoard::Ok) { 00192 if (_conf != NULL) { 00193 free(_conf); 00194 _conf = NULL; 00195 _confSize = 0; 00196 } 00197 } 00198 00199 return err; 00200 } 00201 00202 DMBoard::BoardError BiosLoader::params(bios_header_t** header, void** instanceData) 00203 { 00204 if (!_initialized) { 00205 DMBoard::BoardError err = init(); 00206 if (err != DMBoard::Ok) { 00207 return err; 00208 } 00209 } 00210 if (_initialized) { 00211 *header = &_bios; 00212 *instanceData = _biosData; 00213 return DMBoard::Ok; 00214 } else { 00215 return DMBoard::BiosInvalidError; 00216 } 00217 } 00218 00219 DMBoard::BoardError BiosLoader::init() 00220 { 00221 DMBoard::BoardError err = DMBoard::Ok; 00222 if (!_initialized) { 00223 do { 00224 00225 // Get the display bios from the DMBoard. DMBoard will have verified it 00226 // and will keep it in RAM so there is no need to copy it. 00227 uint8_t* p = NULL; 00228 uint32_t size = 0; 00229 err = readBIOS(&p, &size); 00230 if (err != DMBoard::Ok) { 00231 break; 00232 } 00233 00234 #if defined(MAC_IN_SDK) 00235 // The BIOS has been read so we know that the I2C bus is working. After the 00236 // BIOS is "started" it will take ownership of the bus and it can cause 00237 // problems for other peripherals on it. The only other peripheral today 00238 // is the EEPROM with the MAC address. It is read by mbed_mac_address() in 00239 // ethernet_api.c in the SDK and it will be cached. By reading it here now 00240 // we prevent future access to the I2C bus. 00241 char mac[6] = {0}; 00242 mbed_mac_address(mac); 00243 #endif 00244 00245 // Extract the function pointers so that they can be modified to match the 00246 // actual location of the code 00247 file_header_t* file_header = (file_header_t*)p; 00248 memcpy(&_bios, &file_header->header, sizeof(bios_header_t)); 00249 00250 // Allocate memory for the BIOS instance data 00251 _biosData = malloc(file_header->paramSize); 00252 if (_biosData == NULL) { 00253 err = DMBoard::MemoryError; 00254 break; 00255 } 00256 00257 // All offsets must be moved by two factors: 00258 // 1) The position of the code in RAM (location of "p") 00259 // 2) The header size (the code/data comes after it) 00260 uint32_t offset = ((uint32_t)p) + file_header->headerSize; 00261 uint32_t* functions = (uint32_t*)&_bios; 00262 for (uint32_t i = 0; i < (sizeof(bios_header_t)/sizeof(uint32_t)); i++) { 00263 functions[i] += offset; 00264 } 00265 00266 #if defined(DM_BOARD_BIOS_DEVELOPMENT) 00267 // This requires that the project contains the source code for the BIOS 00268 const char* msg; 00269 uint32_t tmp = 0; 00270 bios_debug_aid(&_bios, &msg, &tmp); 00271 if (tmp > file_header->paramSize) { 00272 free(_biosData); 00273 _biosData = malloc(tmp); 00274 if (_biosData == NULL) { 00275 err = DMBoard::MemoryError; 00276 break; 00277 } 00278 } 00279 DMBoard::instance().logger()->printf("BIOS info: %s\n", msg); 00280 #endif 00281 00282 // Prepare the BIOS instance data before calling the first function 00283 BiosError_t e = _bios.initParams(_biosData, SystemCoreClock, PeripheralClock, wait_us, readTimeMs); 00284 if (e != BiosError_Ok) { 00285 err = DMBoard::BiosInvalidError; 00286 break; 00287 } 00288 00289 // Setup the mandatory I2C0 interrupt handler after initParams but before all other calls 00290 NVIC_DisableIRQ(I2C0_IRQn); 00291 NVIC_SetVector(I2C0_IRQn, (uint32_t)loader_i2c0_irq_handler); 00292 NVIC_EnableIRQ(I2C0_IRQn); 00293 00294 _initialized = true; 00295 } while(0); 00296 } 00297 return err; 00298 } 00299 00300 void BiosLoader::resetI2C() 00301 { 00302 DMBoard::instance().logger()->printf("BiosLoader::resetI2C()\n"); 00303 DigitalOut reset(P0_23); 00304 reset = 0; 00305 wait_ms(1); 00306 reset = 1; 00307 wait_ms(10); 00308 } 00309 00310 00311 /****************************************************************************** 00312 * Public Functions 00313 *****************************************************************************/ 00314 00315 bool BiosLoader::isKnownSPIFIMemory(uint8_t mfgr, uint8_t devType, uint8_t devID, uint32_t memSize, uint32_t* eraseBlockSize) 00316 { 00317 if (!_initialized) { 00318 DMBoard::BoardError err = init(); 00319 if (err != DMBoard::Ok) { 00320 return false; 00321 } 00322 } 00323 if (_initialized) { 00324 bool known = false; 00325 BiosError_t err = _bios.spifiIsSupported(_biosData, mfgr,devType,devID,memSize,&known,eraseBlockSize); 00326 if (err == BiosError_Ok) { 00327 return known; 00328 } 00329 } 00330 return false; 00331 } 00332 00333 void BiosLoader::getMacAddress(char mac[6]) 00334 { 00335 if (!_initialized) { 00336 init(); 00337 } 00338 if (_initialized) { 00339 BiosError_t err = _bios.ethernetMac(_biosData, mac); 00340 if (err == BiosError_Ok) { 00341 return; 00342 } 00343 } 00344 00345 // We always consider the MAC address to be retrieved even though 00346 // reading is failed. If it wasn't possible to read then the default 00347 // address will be used. 00348 mac[0] = 0x00; 00349 mac[1] = 0x02; 00350 mac[2] = 0xF7; 00351 mac[3] = 0xF0; 00352 mac[4] = 0x00; 00353 mac[5] = 0x01; 00354 } 00355 00356 void BiosLoader::handleI2CInterrupt() 00357 { 00358 _bios.i2cIRQHandler(_biosData); 00359 } 00360 00361 void BiosLoader::getBiosStats(uint8_t& type, uint8_t& major, uint8_t& minor, uint8_t& rev) 00362 { 00363 type = (_stats >> 24) & 0xff; 00364 major = (_stats >> 16) & 0xff; 00365 minor = (_stats >> 8) & 0xff; 00366 rev = (_stats >> 0) & 0xff; 00367 } 00368
Generated on Tue Jul 12 2022 21:29:01 by
