ReactOS 0.4.15-dev-8219-ge8b88cf
main.c
Go to the documentation of this file.
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 */
16PCHAR NandImageName = "reactos.bin";
17PCHAR LlbImageName = "./output-arm/boot/armllb/armllb.bin";
18PCHAR BootLdrImageName = "./output-arm/boot/freeldr/freeldr/freeldr.sys";
19PCHAR FsImageName = "ramdisk.img";
20PCHAR RamImageName = "ramdisk.bin";
21
22/* NAND On-Disk Memory Map */
23ULONG LlbStart = 0x00000000, LlbEnd = 0x00010000; // 64 KB
24ULONG BootLdrStart = 0x00010000, BootLdrEnd = 0x00090000; // 512 KB
25ULONG FsStart = 0x00090000, FsEnd = 0x10000000; // 255 MB
26
27/* Should we write OOB data? */
29
30/* FUNCTIONS ******************************************************************/
31
32INT
35{
36 INT FileDescriptor, i;
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
58VOID
60WriteToFlash(IN INT NandImageFile,
62 IN ULONG ImageStart,
63 IN ULONG ImageEnd)
64{
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 */
79
80 /* Create zero buffer */
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 */
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
103VOID
104NTAPI
105WriteLlb(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
115VOID
116NTAPI
117WriteBootLdr(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
127VOID
128NTAPI
129WriteFileSystem(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
139VOID
140NTAPI
141WriteRamDisk(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
151int
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 */
unsigned char BOOLEAN
static int argc
Definition: ServiceArgs.c:12
#define O_CREAT
Definition: acwin.h:110
#define read
Definition: acwin.h:96
#define open
Definition: acwin.h:95
#define close
Definition: acwin.h:98
#define write
Definition: acwin.h:97
Definition: bufpool.h:45
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
int main()
Definition: test.c:6
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static PWSTR ImageFile
Definition: imagefile.c:10
#define O_RDWR
Definition: fcntl.h:36
#define SEEK_SET
Definition: jmemansi.c:26
#define lseek
Definition: syshdrs.h:47
#define argv
Definition: mplay32.c:18
#define NAND_PAGES
Definition: nandflash.h:19
#define NAND_PAGE_SIZE
Definition: nandflash.h:17
#define NAND_OOB_SIZE
Definition: nandflash.h:18
#define exit(n)
Definition: config.h:202
#define memset(x, y, z)
Definition: compat.h:39
VOID NTAPI WriteBootLdr(IN INT NandImageFile)
Definition: main.c:117
ULONG LlbEnd
Definition: main.c:23
PCHAR FsImageName
Definition: main.c:19
PCHAR BootLdrImageName
Definition: main.c:18
PCHAR RamImageName
Definition: main.c:20
ULONG FsStart
Definition: main.c:25
VOID NTAPI WriteToFlash(IN INT NandImageFile, IN INT ImageFile, IN ULONG ImageStart, IN ULONG ImageEnd)
Definition: main.c:60
ULONG NeedsOob
Definition: main.c:28
VOID NTAPI WriteRamDisk(IN INT RamDiskFile)
Definition: main.c:141
ULONG FsEnd
Definition: main.c:25
ULONG LlbStart
Definition: main.c:23
VOID NTAPI WriteLlb(IN INT NandImageFile)
Definition: main.c:105
ULONG BootLdrStart
Definition: main.c:24
ULONG BootLdrEnd
Definition: main.c:24
VOID NTAPI WriteFileSystem(IN INT NandImageFile)
Definition: main.c:129
INT NTAPI CreateFlashFile(VOID)
Definition: main.c:34
PCHAR NandImageName
Definition: main.c:16
PCHAR LlbImageName
Definition: main.c:17
#define NTAPI
Definition: typedefs.h:36
int32_t INT
Definition: typedefs.h:58
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
int WINAPI EndPage(_In_ HDC)
int WINAPI StartPage(_In_ HDC)
char CHAR
Definition: xmlstorage.h:175