1 /* 2 * PROJECT: OMAP3 NAND Flashing Utility 3 * LICENSE: BSD - See COPYING.ARM in the top level directory 4 * FILE: tools/nandflash/main.c 5 * PURPOSE: Flashes OmapLDR, FreeLDR and a Root FS into a NAND image 6 * PROGRAMMERS: ReactOS Portable Systems Group 7 */ 8 9 /* INCLUDES *******************************************************************/ 10 11 #include "nandflash.h" 12 13 /* GLOBALS ********************************************************************/ 14 15 /* File Names */ 16 PCHAR NandImageName = "reactos.bin"; 17 PCHAR LlbImageName = "./output-arm/boot/armllb/armllb.bin"; 18 PCHAR BootLdrImageName = "./output-arm/boot/freeldr/freeldr/freeldr.sys"; 19 PCHAR FsImageName = "ramdisk.img"; 20 PCHAR RamImageName = "ramdisk.bin"; 21 22 /* NAND On-Disk Memory Map */ 23 ULONG LlbStart = 0x00000000, LlbEnd = 0x00010000; // 64 KB 24 ULONG BootLdrStart = 0x00010000, BootLdrEnd = 0x00090000; // 512 KB 25 ULONG FsStart = 0x00090000, FsEnd = 0x10000000; // 255 MB 26 27 /* Should we write OOB data? */ 28 ULONG NeedsOob = TRUE; 29 30 /* FUNCTIONS ******************************************************************/ 31 32 INT 33 NTAPI 34 CreateFlashFile(VOID) 35 { 36 INT FileDescriptor, i; 37 CHAR Buffer[NAND_PAGE_SIZE + NAND_OOB_SIZE]; 38 39 /* Try open NAND image */ 40 FileDescriptor = open(NandImageName, O_RDWR); 41 if (FileDescriptor) 42 { 43 /* Create NAND image */ 44 FileDescriptor = open(NandImageName, O_RDWR | O_CREAT); 45 if (FileDescriptor) return FileDescriptor; 46 47 /* Create zero buffer */ 48 memset(Buffer, 0xff, sizeof(Buffer)); 49 50 /* Write zero buffer */ 51 for (i = 0; i < NAND_PAGES; i++) write(FileDescriptor, Buffer, sizeof(Buffer)); 52 } 53 54 /* Return NAND descriptor */ 55 return FileDescriptor; 56 } 57 58 VOID 59 NTAPI 60 WriteToFlash(IN INT NandImageFile, 61 IN INT ImageFile, 62 IN ULONG ImageStart, 63 IN ULONG ImageEnd) 64 { 65 CHAR Data[NAND_PAGE_SIZE], Oob[NAND_OOB_SIZE]; 66 ULONG StartPage, EndPage, i, OobSize = 0; 67 BOOLEAN KeepGoing = TRUE; 68 69 /* Offset to NAND Page convert */ 70 StartPage = ImageStart / NAND_PAGE_SIZE; 71 EndPage = ImageEnd / NAND_PAGE_SIZE; 72 73 /* Jump to NAND offset */ 74 if (NeedsOob) OobSize = NAND_OOB_SIZE; 75 lseek(NandImageFile, StartPage * (NAND_PAGE_SIZE + OobSize), SEEK_SET); 76 77 /* Set input image offset */ 78 lseek(ImageFile, 0, SEEK_SET); 79 80 /* Create zero buffer */ 81 memset(Data, 0xff, NAND_PAGE_SIZE); 82 memset(Oob, 0xff, NAND_OOB_SIZE); 83 84 /* Parse NAND Pages */ 85 for (i = StartPage; i < EndPage; i++) 86 { 87 /* Read NAND page from input image */ 88 if (read(ImageFile, Data, NAND_PAGE_SIZE) < NAND_PAGE_SIZE) 89 { 90 /* Do last write and quit after */ 91 KeepGoing = FALSE; 92 } 93 94 /* Write OOB and NAND Data */ 95 write(NandImageFile, Data, NAND_PAGE_SIZE); 96 if (NeedsOob) write(NandImageFile, Oob, NAND_OOB_SIZE); 97 98 /* Next page if data continues */ 99 if (!KeepGoing) break; 100 } 101 } 102 103 VOID 104 NTAPI 105 WriteLlb(IN INT NandImageFile) 106 { 107 INT FileDescriptor; 108 109 /* Open LLB and write it */ 110 FileDescriptor = open(LlbImageName, O_RDWR); 111 WriteToFlash(NandImageFile, FileDescriptor, LlbStart, LlbEnd); 112 close(FileDescriptor); 113 } 114 115 VOID 116 NTAPI 117 WriteBootLdr(IN INT NandImageFile) 118 { 119 INT FileDescriptor; 120 121 /* Open FreeLDR and write it */ 122 FileDescriptor = open(BootLdrImageName, O_RDWR); 123 WriteToFlash(NandImageFile, FileDescriptor, BootLdrStart, BootLdrEnd); 124 close(FileDescriptor); 125 } 126 127 VOID 128 NTAPI 129 WriteFileSystem(IN INT NandImageFile) 130 { 131 INT FileDescriptor; 132 133 /* Open FS image and write it */ 134 FileDescriptor = open(FsImageName, O_RDWR); 135 WriteToFlash(NandImageFile, FileDescriptor, FsStart, FsEnd); 136 close(FileDescriptor); 137 } 138 139 VOID 140 NTAPI 141 WriteRamDisk(IN INT RamDiskFile) 142 { 143 INT FileDescriptor; 144 145 /* Open FS image and write it 16MB later */ 146 FileDescriptor = open(FsImageName, O_RDWR); 147 WriteToFlash(RamDiskFile, FileDescriptor, 16 * 1024 * 1024, (32 + 16) * 1024 * 1024); 148 close(FileDescriptor); 149 } 150 151 int 152 main(ULONG argc, 153 char **argv) 154 { 155 INT NandImageFile, RamDiskFile; 156 157 /* Flat NAND, no OOB */ 158 if (argc == 2) NeedsOob = FALSE; 159 160 /* Open or create NAND Image File */ 161 NandImageFile = CreateFlashFile(); 162 if (!NandImageFile) exit(-1); 163 164 /* Write components */ 165 WriteLlb(NandImageFile); 166 WriteBootLdr(NandImageFile); 167 if (NeedsOob) 168 { 169 /* Write the ramdisk normaly */ 170 WriteFileSystem(NandImageFile); 171 } 172 else 173 { 174 /* Open a new file for the ramdisk */ 175 RamDiskFile = open(RamImageName, O_RDWR | O_CREAT); 176 if (!RamDiskFile) exit(-1); 177 178 /* Write it */ 179 WriteRamDisk(RamDiskFile); 180 181 /* Close */ 182 close(RamDiskFile); 183 } 184 185 186 /* Close and return */ 187 close(NandImageFile); 188 return 0; 189 } 190 191 /* EOF */ 192