📄 loader.c
字号:
#include <stdlib.h>
#include <string.h>
#include "option.h"
#include "s2440addr.h"
#include "def.h"
#include "utils.h"
#include "nand.h"
// Constants.
// #define NAND_BLOCK_SIZE_BYTES 0x00020000
#define NAND_BLOCK_SIZE_BYTES 0x00004000 // 16K/Page
#define NAND_PAGE_SIZE_BYTES 0x00000200 // 512Bytes/Page
#define NAND_PAGES_PER_BLOCK (NAND_BLOCK_SIZE_BYTES / NAND_PAGE_SIZE_BYTES)
// NOTE: we assume that this Steppingstone loader occupies *part* the first (good) NAND flash block. More
// specifically, the loader takes up 4096 bytes (or 8 NAND pages) of the first block. We'll start our image
// copy on the very next page.
#define NAND_COPY_PAGE_OFFSET 2*NAND_PAGES_PER_BLOCK
#define LOAD_ADDRESS_PHYSICAL 0x30038000
#define LOAD_SIZE_BYTES 0x00040000
#define LOAD_SIZE_PAGES (LOAD_SIZE_BYTES / NAND_PAGE_SIZE_BYTES)
typedef void (*PFN_IMAGE_LAUNCH)();
/*
@func void | main | C entrypoint function for the Steppingstone loader.
@rdesc None.
@comm
@xref
*/
void Main(void)
{
register nBlock;
register nPage;
register nBadBlocks;
volatile BYTE *pCopyPtr;
// Enable the ICache.
MMU_EnableICache();
// Set up all GPIO ports.
Port_Init();
// Initialize UART for debug
Uart_Init();
Uart_SendString("Loading......\r\n");
// Initialize the NAND flash interface.
NF_Init();
// Copy image from NAND flash to RAM.
pCopyPtr = (BYTE *)LOAD_ADDRESS_PHYSICAL;
// NOTE: we assume that this Steppingstone loader occupies *part* the first (good) NAND flash block. More
// specifically, the loader takes up 4096 bytes (or 8 NAND pages) of the first block. We'll start our image
// copy on the very next page.
FlashID = NF_ReadID();
if (!FlashID)
{
Uart_SendString("This NandFlash Type is not supported!!\r\n Booting is failure \r\n");
while(1)
{
;
}
}
nBadBlocks = 0;
for (nPage = NAND_COPY_PAGE_OFFSET ; nPage < (LOAD_SIZE_PAGES + NAND_COPY_PAGE_OFFSET) ; nPage++)
{
nBlock = ((nPage / NAND_PAGES_PER_BLOCK) + nBadBlocks);
if (!NF_ReadPage(nBlock, (nPage % NAND_PAGES_PER_BLOCK), pCopyPtr))
{
if ((nPage % NAND_PAGES_PER_BLOCK) != 0)
{
// Spin forever...
while(1)
{
;
}
}
// ECC error on a block boundary is (likely) a bad block - retry the page 0 read on the next block.
nBadBlocks++;
nPage--;
continue;
}
Uart_SendString("*");
pCopyPtr += NAND_PAGE_SIZE_BYTES;
}
Uart_SendString("\r\nRunning......\r\n");
// Jump to the image...
((PFN_IMAGE_LAUNCH)(LOAD_ADDRESS_PHYSICAL))();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -