2010-02-01 18:29:19 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: OMAP3 NAND Flashing Utility
|
|
|
|
* LICENSE: BSD - See COPYING.ARM in the top level directory
|
|
|
|
* FILE: tools/nandflash/main.c
|
|
|
|
* PURPOSE: Flashes OmapLDR, FreeLDR and a Root FS into a NAND image
|
|
|
|
* PROGRAMMERS: ReactOS Portable Systems Group
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
|
|
|
#include "nandflash.h"
|
|
|
|
|
|
|
|
/* GLOBALS ********************************************************************/
|
|
|
|
|
|
|
|
/* File Names */
|
|
|
|
PCHAR NandImageName = "reactos.bin";
|
|
|
|
PCHAR LlbImageName = "./output-arm/boot/armllb/armllb.bin";
|
2010-02-03 23:16:26 +00:00
|
|
|
PCHAR BootLdrImageName = "./output-arm/boot/freeldr/freeldr/freeldr.sys";
|
2010-02-04 19:49:25 +00:00
|
|
|
PCHAR FsImageName = "ramdisk.img";
|
|
|
|
PCHAR RamImageName = "ramdisk.bin";
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* NAND On-Disk Memory Map */
|
2010-02-03 23:16:26 +00:00
|
|
|
ULONG LlbStart = 0x00000000, LlbEnd = 0x00010000; // 64 KB
|
|
|
|
ULONG BootLdrStart = 0x00010000, BootLdrEnd = 0x00090000; // 512 KB
|
|
|
|
ULONG FsStart = 0x00090000, FsEnd = 0x10000000; // 255 MB
|
|
|
|
|
|
|
|
/* Should we write OOB data? */
|
|
|
|
ULONG NeedsOob = TRUE;
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS ******************************************************************/
|
|
|
|
|
2010-02-03 23:16:26 +00:00
|
|
|
INT
|
2010-02-01 18:29:19 +00:00
|
|
|
NTAPI
|
|
|
|
CreateFlashFile(VOID)
|
|
|
|
{
|
2010-02-03 23:16:26 +00:00
|
|
|
INT FileDescriptor, i;
|
2010-02-01 18:29:19 +00:00
|
|
|
CHAR Buffer[NAND_PAGE_SIZE + NAND_OOB_SIZE];
|
|
|
|
|
|
|
|
/* Try open NAND image */
|
|
|
|
FileDescriptor = open(NandImageName, O_RDWR);
|
2010-02-03 23:16:26 +00:00
|
|
|
if (FileDescriptor)
|
2010-02-01 18:29:19 +00:00
|
|
|
{
|
|
|
|
/* Create NAND image */
|
|
|
|
FileDescriptor = open(NandImageName, O_RDWR | O_CREAT);
|
2010-02-03 23:16:26 +00:00
|
|
|
if (FileDescriptor) return FileDescriptor;
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* Create zero buffer */
|
|
|
|
memset(Buffer, 0xff, sizeof(Buffer));
|
|
|
|
|
|
|
|
/* Write zero buffer */
|
|
|
|
for (i = 0; i < NAND_PAGES; i++) write(FileDescriptor, Buffer, sizeof(Buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return NAND descriptor */
|
|
|
|
return FileDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2010-02-03 23:16:26 +00:00
|
|
|
WriteToFlash(IN INT NandImageFile,
|
|
|
|
IN INT ImageFile,
|
2010-02-01 18:29:19 +00:00
|
|
|
IN ULONG ImageStart,
|
|
|
|
IN ULONG ImageEnd)
|
|
|
|
{
|
|
|
|
CHAR Data[NAND_PAGE_SIZE], Oob[NAND_OOB_SIZE];
|
2010-02-03 23:16:26 +00:00
|
|
|
ULONG StartPage, EndPage, i, OobSize = 0;
|
2010-02-01 18:29:19 +00:00
|
|
|
BOOLEAN KeepGoing = TRUE;
|
|
|
|
|
|
|
|
/* Offset to NAND Page convert */
|
|
|
|
StartPage = ImageStart / NAND_PAGE_SIZE;
|
|
|
|
EndPage = ImageEnd / NAND_PAGE_SIZE;
|
2015-01-07 19:26:49 +00:00
|
|
|
|
2010-02-01 18:29:19 +00:00
|
|
|
/* Jump to NAND offset */
|
2010-02-03 23:16:26 +00:00
|
|
|
if (NeedsOob) OobSize = NAND_OOB_SIZE;
|
|
|
|
lseek(NandImageFile, StartPage * (NAND_PAGE_SIZE + OobSize), SEEK_SET);
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* Set input image offset */
|
|
|
|
lseek(ImageFile, 0, SEEK_SET);
|
|
|
|
|
|
|
|
/* Create zero buffer */
|
|
|
|
memset(Data, 0xff, NAND_PAGE_SIZE);
|
|
|
|
memset(Oob, 0xff, NAND_OOB_SIZE);
|
|
|
|
|
|
|
|
/* Parse NAND Pages */
|
|
|
|
for (i = StartPage; i < EndPage; i++)
|
|
|
|
{
|
|
|
|
/* Read NAND page from input image */
|
|
|
|
if (read(ImageFile, Data, NAND_PAGE_SIZE) < NAND_PAGE_SIZE)
|
|
|
|
{
|
|
|
|
/* Do last write and quit after */
|
|
|
|
KeepGoing = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write OOB and NAND Data */
|
|
|
|
write(NandImageFile, Data, NAND_PAGE_SIZE);
|
2010-02-03 23:16:26 +00:00
|
|
|
if (NeedsOob) write(NandImageFile, Oob, NAND_OOB_SIZE);
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* Next page if data continues */
|
|
|
|
if (!KeepGoing) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2010-02-03 23:16:26 +00:00
|
|
|
WriteLlb(IN INT NandImageFile)
|
2010-02-01 18:29:19 +00:00
|
|
|
{
|
2010-02-03 23:16:26 +00:00
|
|
|
INT FileDescriptor;
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* Open LLB and write it */
|
|
|
|
FileDescriptor = open(LlbImageName, O_RDWR);
|
|
|
|
WriteToFlash(NandImageFile, FileDescriptor, LlbStart, LlbEnd);
|
|
|
|
close(FileDescriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2010-02-03 23:16:26 +00:00
|
|
|
WriteBootLdr(IN INT NandImageFile)
|
2010-02-01 18:29:19 +00:00
|
|
|
{
|
2010-02-03 23:16:26 +00:00
|
|
|
INT FileDescriptor;
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* Open FreeLDR and write it */
|
|
|
|
FileDescriptor = open(BootLdrImageName, O_RDWR);
|
|
|
|
WriteToFlash(NandImageFile, FileDescriptor, BootLdrStart, BootLdrEnd);
|
|
|
|
close(FileDescriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2010-02-03 23:16:26 +00:00
|
|
|
WriteFileSystem(IN INT NandImageFile)
|
2010-02-01 18:29:19 +00:00
|
|
|
{
|
2010-02-03 23:16:26 +00:00
|
|
|
INT FileDescriptor;
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* Open FS image and write it */
|
|
|
|
FileDescriptor = open(FsImageName, O_RDWR);
|
|
|
|
WriteToFlash(NandImageFile, FileDescriptor, FsStart, FsEnd);
|
|
|
|
close(FileDescriptor);
|
|
|
|
}
|
|
|
|
|
2010-02-04 19:49:25 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
WriteRamDisk(IN INT RamDiskFile)
|
|
|
|
{
|
|
|
|
INT FileDescriptor;
|
|
|
|
|
|
|
|
/* Open FS image and write it 16MB later */
|
|
|
|
FileDescriptor = open(FsImageName, O_RDWR);
|
|
|
|
WriteToFlash(RamDiskFile, FileDescriptor, 16 * 1024 * 1024, (32 + 16) * 1024 * 1024);
|
|
|
|
close(FileDescriptor);
|
|
|
|
}
|
|
|
|
|
2010-02-01 18:29:19 +00:00
|
|
|
int
|
|
|
|
main(ULONG argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
2010-02-04 19:49:25 +00:00
|
|
|
INT NandImageFile, RamDiskFile;
|
2015-01-07 19:26:49 +00:00
|
|
|
|
2010-02-03 23:16:26 +00:00
|
|
|
/* Flat NAND, no OOB */
|
|
|
|
if (argc == 2) NeedsOob = FALSE;
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* Open or create NAND Image File */
|
|
|
|
NandImageFile = CreateFlashFile();
|
|
|
|
if (!NandImageFile) exit(-1);
|
|
|
|
|
|
|
|
/* Write components */
|
|
|
|
WriteLlb(NandImageFile);
|
|
|
|
WriteBootLdr(NandImageFile);
|
2010-02-04 19:49:25 +00:00
|
|
|
if (NeedsOob)
|
|
|
|
{
|
|
|
|
/* Write the ramdisk normaly */
|
|
|
|
WriteFileSystem(NandImageFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Open a new file for the ramdisk */
|
|
|
|
RamDiskFile = open(RamImageName, O_RDWR | O_CREAT);
|
|
|
|
if (!RamDiskFile) exit(-1);
|
|
|
|
|
|
|
|
/* Write it */
|
|
|
|
WriteRamDisk(RamDiskFile);
|
2015-01-07 19:26:49 +00:00
|
|
|
|
2010-02-04 19:49:25 +00:00
|
|
|
/* Close */
|
|
|
|
close(RamDiskFile);
|
|
|
|
}
|
|
|
|
|
2010-02-01 18:29:19 +00:00
|
|
|
|
|
|
|
/* Close and return */
|
|
|
|
close(NandImageFile);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|