ReactOS  0.4.15-dev-1386-g5cb9f87
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 */
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? */
29 
30 /* FUNCTIONS ******************************************************************/
31 
32 INT
33 NTAPI
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 
58 VOID
59 NTAPI
60 WriteToFlash(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 */
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 */
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
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 */
signed char * PCHAR
Definition: retypes.h:7
static PWSTR ImageFile
Definition: imagefile.c:10
static int argc
Definition: ServiceArgs.c:12
#define IN
Definition: typedefs.h:39
INT NTAPI CreateFlashFile(VOID)
Definition: main.c:34
#define NAND_PAGE_SIZE
Definition: nandflash.h:17
#define open
Definition: acwin.h:95
#define TRUE
Definition: types.h:120
char CHAR
Definition: xmlstorage.h:175
PCHAR BootLdrImageName
Definition: main.c:18
_Check_return_opt_ _CRTIMP long __cdecl lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
#define argv
Definition: mplay32.c:18
int32_t INT
Definition: typedefs.h:58
ULONG FsEnd
Definition: main.c:25
int WINAPI EndPage(_In_ HDC)
int WINAPI StartPage(_In_ HDC)
#define NAND_OOB_SIZE
Definition: nandflash.h:18
#define write
Definition: acwin.h:97
PCHAR FsImageName
Definition: main.c:19
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
unsigned char BOOLEAN
PCHAR RamImageName
Definition: main.c:20
Definition: bufpool.h:45
VOID NTAPI WriteFileSystem(IN INT NandImageFile)
Definition: main.c:129
#define SEEK_SET
Definition: jmemansi.c:26
ULONG NeedsOob
Definition: main.c:28
ULONG FsStart
Definition: main.c:25
int main(int argc, const char *argv[])
Definition: main.c:122
ULONG LlbStart
Definition: main.c:23
PCHAR LlbImageName
Definition: main.c:17
#define NAND_PAGES
Definition: nandflash.h:19
#define close
Definition: acwin.h:98
ULONG LlbEnd
Definition: main.c:23
ULONG BootLdrEnd
Definition: main.c:24
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
VOID NTAPI WriteLlb(IN INT NandImageFile)
Definition: main.c:105
#define O_RDWR
Definition: fcntl.h:36
VOID NTAPI WriteToFlash(IN INT NandImageFile, IN INT ImageFile, IN ULONG ImageStart, IN ULONG ImageEnd)
Definition: main.c:60
ULONG BootLdrStart
Definition: main.c:24
unsigned int ULONG
Definition: retypes.h:1
void exit(int exitcode)
Definition: _exit.c:33
#define memset(x, y, z)
Definition: compat.h:39
PCHAR NandImageName
Definition: main.c:16
VOID NTAPI WriteBootLdr(IN INT NandImageFile)
Definition: main.c:117
#define O_CREAT
Definition: acwin.h:110
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
VOID NTAPI WriteRamDisk(IN INT RamDiskFile)
Definition: main.c:141