1 /*---------------------------------------------------------------------------*
2 Project: CARD API save demo
3 File: save.c
4
5 Copyright 2001 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Log: save.c,v $
14 Revision 1.3 2007/07/03 04:47:31 mitu
15 added the message of the end of the program.
16
17 Revision 1.2 2006/02/20 04:13:08 mitu
18 changed include path from dolphin/ to revolution/.
19
20 Revision 1.1 2006/01/31 10:48:30 mitu
21 (none)
22
23
24 2 9/03/01 20:23 Shiki
25 Modified to use slot B.
26
27 1 6/14/01 11:36a Shiki
28 Initial check-in.
29 $NoKeywords: $
30 *---------------------------------------------------------------------------*/
31
32 #include <revolution.h>
33
34 #define OFFSET(n, a) (((u32) (n)) & ((a) - 1))
35 #define TRUNC(n, a) (((u32) (n)) & ~((a) - 1))
36 #define ROUND(n, a) (((u32) (n) + (a) - 1) & ~((a) - 1))
37
38 #define CARD_CHAN 1
39
40 u8 CardWorkArea[CARD_WORKAREA_SIZE] ATTRIBUTE_ALIGN(32);
41
LoadFile(char * name,s32 * length)42 static void* LoadFile(char* name, s32* length)
43 {
44 DVDFileInfo fileInfo;
45 BOOL result;
46 void* data;
47
48 result = DVDOpen(name, &fileInfo);
49 if (!result)
50 {
51 return NULL;
52 }
53 *length = (s32) OSRoundUp32B(DVDGetLength(&fileInfo));
54 data = OSAllocFromArenaLo((u32) *length, 32);
55 result = DVDRead(&fileInfo, data, *length, 0);
56 if (!result)
57 {
58 return NULL;
59 }
60 DVDClose(&fileInfo);
61
62 return data;
63 }
64
main(int argc,char * argv[])65 int main(int argc, char* argv[])
66 {
67 s32 result;
68 CARDFileInfo fileInfo;
69 CARDStat stat;
70 u32 sectorSize;
71 s32 length;
72 void* data;
73
74 OSInit();
75 DVDInit();
76 CARDInit();
77
78 if (argc != 2)
79 {
80 OSHalt("usage: save filename");
81 }
82
83 data = LoadFile(argv[1], &length);
84 if (data == NULL)
85 {
86 OSHalt("error: could not load file from disk");
87 }
88
89 // Probe
90 OSReport("Insert card in slot %c.\n", "AB"[CARD_CHAN]);
91 while (!CARDProbe(CARD_CHAN))
92 {
93 ;
94 }
95
96 // Mount
97 OSReport("CARDMount ");
98 result = CARDMountAsync(CARD_CHAN, CardWorkArea, 0, 0);
99 if (result < 0)
100 {
101 OSReport("failed. (%d)\n", result);
102 return 1;
103 }
104 while ((result = CARDGetResultCode(CARD_CHAN)) == CARD_RESULT_BUSY)
105 {
106 ;
107 }
108 switch (result)
109 {
110 case CARD_RESULT_READY:
111 case CARD_RESULT_BROKEN:
112 OSReport("done. (%d)\n", result);
113 OSReport("CARDCheck ");
114 result = CARDCheckAsync(CARD_CHAN, 0);
115 if (result < 0)
116 {
117 CARDUnmount(CARD_CHAN);
118 OSReport("failed. (%d)\n", result);
119 return 1;
120 }
121 while ((result = CARDGetResultCode(CARD_CHAN)) == CARD_RESULT_BUSY)
122 {
123 ;
124 }
125 break;
126 }
127 switch (result)
128 {
129 case CARD_RESULT_READY:
130 OSReport("done. (%d)\n", result);
131 break;
132 case CARD_RESULT_BROKEN:
133 case CARD_RESULT_ENCODING:
134 CARDUnmount(CARD_CHAN);
135 OSReport("failed. (%d)\n", result);
136 return 1;
137 break;
138 default:
139 OSReport("failed. (%d)\n", result);
140 return 1;
141 break;
142 }
143
144 // Sector size
145 result = CARDGetSectorSize(CARD_CHAN, §orSize);
146 if (result < 0)
147 {
148 OSReport("CARDGetSectorSize() failed. (%d)\n", result);
149 return 1;
150 }
151 OSReport("Sector size %d bytes.\n", sectorSize);
152
153 // Adjust length
154 length = (s32) ROUND(length, sectorSize);
155
156 // Delete
157 OSReport("CARDDelete ");
158 result = CARDDelete(CARD_CHAN, argv[1]);
159 OSReport("done. (%d)\n", result);
160
161 // Create
162 OSReport("CARDCreate ");
163 result = CARDCreateAsync(CARD_CHAN, argv[1], (u32) length, &fileInfo, 0);
164 if (result < 0)
165 {
166 OSReport("failed. (%d)\n", result);
167 return 1;
168 }
169 while ((result = CARDGetResultCode(CARD_CHAN)) == CARD_RESULT_BUSY)
170 {
171 ;
172 }
173 OSReport("done. (%d:%d:%d)\n", result, fileInfo.chan, fileInfo.fileNo);
174
175 // Stat
176 OSReport("CARDGetStatus ");
177 result = CARDGetStatus(CARD_CHAN, fileInfo.fileNo, &stat);
178 if (result < 0)
179 {
180 OSReport("failed. (%d)\n", result);
181 return 1;
182 }
183 OSReport("gameName %.4s\n", stat.gameName);
184 OSReport("company %.2s\n", stat.company);
185 OSReport("length %d\n", stat.length);
186 OSReport("fileName %.32s\n", stat.fileName);
187 OSReport("time %d\n", stat.time);
188
189 OSReport("CARDWrite ");
190 result = CARDWriteAsync(&fileInfo, data, length, 0, 0);
191 if (result < 0)
192 {
193 OSReport("failed. (%d)\n", result);
194 return 1;
195 }
196 while ((result = CARDGetResultCode(CARD_CHAN)) == CARD_RESULT_BUSY)
197 {
198 ;
199 }
200 if (result < 0)
201 {
202 OSReport("failed. (%d)\n", result);
203 return 1;
204 }
205 OSReport("done. (%d)\n", result);
206
207 // Close
208 CARDClose(&fileInfo);
209
210 // Unmount
211 CARDUnmount(CARD_CHAN);
212
213 OSReport("End of program\n");
214
215 return 0;
216 }
217