1 /*---------------------------------------------------------------------------*
2   Project:      Sample demo program for NAND library
3   File:         basic.c
4   Programmer:   HIRATSU Daisuke
5 
6   Copyright (C) 2006 Nintendo.  All rights reserved.
7 
8   These coded instructions, statements, and computer programs contain
9   proprietary information of Nintendo of America Inc. and/or Nintendo
10   Company Ltd., and are protected by Federal copyright law.  They may
11   not be disclosed to third parties or copied or duplicated in any form,
12   in whole or in part, without the prior written consent of Nintendo.
13 
14   $Log: basic.c,v $
15   Revision 1.11  06/05/2006 13:09:05  hiratsu
16   Removed NANDFinalize().
17   Used home directory to store file.
18 
19   Revision 1.10  05/03/2006 09:07:25  hiratsu
20   Fixed a little bit because NAND APIs are changed.
21 
22   Revision 1.9  2006/04/27 17:57:35  orest
23   Added NANDFinalize() api.
24 
25   Revision 1.8  03/17/2006 02:41:09  hiratsu
26   Now this sample demo program uses buffer from MEM2 with 32byte alignment.
27 
28   Revision 1.7  03/11/2006 10:43:20  hiratsu
29   Now, we can link real ISFS module.
30 
31   Revision 1.6  03/06/2006 12:05:34  hiratsu
32   Removed finalize function.
33 
34   Revision 1.5  2006/03/06 09:59:08  kawaset
35   Eliminated warnings.
36 
37   Revision 1.4  02/16/2006 13:00:53  hiratsu
38   Changed order of arguments for NANDOpen().
39 
40   Revision 1.3  02/06/2006 08:07:51  hiratsu
41   Added seek code.
42   Now this demo prints write buffer and read buffer.
43 
44   Revision 1.2  01/30/2006 10:32:34  hiratsu
45   Modified code because specification of NANDInit() has been changed.
46   Added static function for initialize and finalize to make code simple.
47   (I wanted to avoid too many #ifdef)
48 
49   Revision 1.1  01/24/2006 12:34:39  hiratsu
50   NAND library demo program.
51   This tells basic operation of NAND library ( Init -> Create -> Open -> Write -> Read -> Close -> Delete ).
52 
53  *---------------------------------------------------------------------------*/
54 
55 
56 #include <string.h>
57 #include <stdlib.h>
58 #include <revolution.h>
59 #include <revolution/nand.h>
60 
61 #include "util.h"
62 
63 #define FILENAME "nanddemo.dat"
64 #define PERM (NAND_PERM_OWNER_READ | NAND_PERM_OWNER_WRITE)    // Owner can read/write
65 #define ATTR 0x00                                              // No attributes.
66 #define SIZE 256
67 
main(void)68 int main(void)
69 {
70     s32 ret = NAND_RESULT_FATAL_ERROR;
71     NANDFileInfo info;
72     char *data=0;
73     char *buf=0;
74     char path[NAND_MAX_PATH]="";
75 
76     initializeHeap();
77     data = (char*)alloc32(SIZE);
78     buf  = (char*)alloc32(SIZE);
79     strcpy(data, "Sample demo program for NAND library.");
80 
81     ret = NANDInit();
82     if(ret != NAND_RESULT_OK)
83     {
84         printErrMsg(ret);
85         OSReport("NANDInit() failed.\n");
86         return EXIT_FAILURE;
87     }
88 
89     ret = NANDGetCurrentDir(path);
90     if(ret != NAND_RESULT_OK)
91     {
92         printErrMsg(ret);
93         OSReport("NANDGetCurrentDir() failed.\n");
94         return EXIT_FAILURE;
95     }
96     OSReport("Current directory: %s\n", path);
97 
98 
99     ret = NANDCreate(FILENAME, PERM, ATTR);
100     if(ret == NAND_RESULT_OK)
101     {
102         // Do nothing.
103     }
104     else if(ret == NAND_RESULT_EXISTS)
105     {
106         OSReport("File already exists.\n");
107     }
108     else
109     {
110         printErrMsg(ret);
111         OSReport("NANDCreate() failed.\n");
112         return EXIT_FAILURE;
113     }
114 
115     ret = NANDOpen(FILENAME, &info, NAND_ACCESS_WRITE | NAND_ACCESS_READ);
116     if(ret != NAND_RESULT_OK)
117     {
118         printErrMsg(ret);
119         OSReport("NANDOpen() failed.  Result code: %d\n", ret);
120         return EXIT_FAILURE;
121     }
122 
123     ret = NANDWrite(&info, data, SIZE);
124     if(ret != SIZE)
125     {
126         printErrMsg(ret);
127         OSReport("NANDWrite() failed.\n");
128         NANDClose(&info);
129         return EXIT_FAILURE;
130     }
131 
132     ret = NANDSeek(&info, 0, NAND_SEEK_SET);
133     if(ret != 0)
134     {
135         printErrMsg(ret);
136         OSReport("NANDSeek() failed.\n");
137         NANDClose(&info);
138         return EXIT_FAILURE;
139     }
140 
141     ret = NANDRead(&info, buf, SIZE);
142     if(ret != SIZE)
143     {
144         printErrMsg(ret);
145         OSReport("NANDRead() failed.\n");
146         NANDClose(&info);
147         return EXIT_FAILURE;
148     }
149 
150     OSReport("Write data: %s\n", data);
151     OSReport("Read data:  %s\n", buf);
152 
153     ret = NANDClose(&info);
154     if(ret != NAND_RESULT_OK)
155     {
156         printErrMsg(ret);
157         OSReport("NANDClose() failed.\n");
158         return EXIT_FAILURE;
159     }
160 
161     ret = NANDDelete(FILENAME);
162     if(ret != NAND_RESULT_OK)
163     {
164         printErrMsg(ret);
165         OSReport("NANDDelete() failed.\n");
166         return EXIT_FAILURE;
167     }
168 
169     free32(buf);
170     free32(data);
171     finalizeHeap();
172 
173     OSHalt("Finished with success");
174     return EXIT_SUCCESS;     // Never reach here.
175 }
176