forked from Nitrokey/nitrokey-storage-firmware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_io.c
435 lines (311 loc) · 11.1 KB
/
file_io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Author: Copyright (C) Rudolf Boeddeker Date: 26.11.2012
*
* This file is part of Nitrokey
*
* Nitrokey is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Nitrokey is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nitrokey. If not, see <http:https://www.gnu.org/licenses/>.
*/
/*
* file_io.c
*
* Created on: 26.11.2012
* Author: RB
*/
#include "board.h"
#ifdef FREERTOS_USED
#include "FreeRTOS.h"
#include "task.h"
#endif
#include "string.h"
#include "stdio.h"
#include "print_funcs.h"
#include "gpio.h"
#include "pm.h"
#include "adc.h"
#include "time.h"
#include "global.h"
#include "tools.h"
#include "Interpreter.h"
#include "BUFFERED_SIO.h"
#include "FILE_IO/FileAccessInterface.h"
#include "TIME_MEASURING.h"
/* FAT includes */
#include "file.h"
#include "navigation.h"
#include "ctrl_access.h"
#include "fsaccess.h"
#include "Inbetriebnahme.h"
#include "html_io.h"
#include "INTERNAL_WORK/internal_work.h"
#include "HighLevelFunctions/HandleAesStorageKey.h"
#include "DFU_test.h"
#include "SD_Test.h"
/*******************************************************************************
Local defines
*******************************************************************************/
/*
#define HTML_FILEIO_LUN 0 // RAM Disk #define SD_UNCRYPTED_FILEIO_LUN 1 #define SD_CRYPTED_FILEIO_LUN 2 */
#define SD_UNCRYPTED_FILEIO_LUN 0
#define SD_CRYPTED_FILEIO_LUN 1
/*******************************************************************************
Global declarations
*******************************************************************************/
/*******************************************************************************
External declarations
*******************************************************************************/
void sd_mmc_mci_resources_init (void);
/*******************************************************************************
Local declarations
*******************************************************************************/
/*******************************************************************************
SD_UncryptedFileIO_Init_u8
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
u8 SD_UncryptedFileIO_Init_u8 (void)
{
b_fsaccess_init ();
// Mount SD drive
CI_LocalPrintf ("Mount uncrypted SD Card\r\n");
if (FALSE == FAI_mount (SD_UNCRYPTED_FILEIO_LUN))
{
return (FALSE);
}
FAI_UpdateContend (1);
vTaskDelay (100);
// Show free space
// CI_LocalPrintf ("\r\n\r\n");
FAI_free_space (1);
// Show dirs
CI_LocalPrintf ("\r\nOld dir\r\n");
FAI_Dir (0);
return (TRUE);
}
/*******************************************************************************
FileIO_UpdateToHost_v
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
extern Ctrl_status sd_mmc_mci_unit_state_e;
void FileIO_UpdateToHost_v (void)
{
#if LUN_2 == ENABLE
set_virtual_unit_busy (); // Tell the host that the FAT has changed
#endif
sd_mmc_mci_unit_state_e = CTRL_BUSY;
}
/*******************************************************************************
FileIO_CdDir_u8
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
u8 FileIO_CdDir_u8 (u8 * Dir_pu8)
{
// cd dir
CI_LocalPrintf ("cd to -%s-\r\n", Dir_pu8);
if (FALSE == FAI_cd ((u8 *) Dir_pu8))
{
// Make new dir if not present
CI_LocalPrintf ("make dir -%s-\r\n", Dir_pu8);
FAI_mkdir ((u8 *) Dir_pu8);
CI_LocalPrintf ("cd to -%s-\r\n", Dir_pu8);
if (FALSE == FAI_cd ((u8 *) Dir_pu8))
{
CI_LocalPrintf ("cd to -%s- fails\r\n", Dir_pu8);
return (FALSE);
}
}
return (TRUE);
}
/*******************************************************************************
FileIO_AppendText_u8
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
u8 FileIO_AppendText_u8 (u8 * Filename_pu8, u8 * Text_pu8)
{
s32 FileID_s32;
s32 Ret_s32;
FileID_s32 = open ((char *) Filename_pu8, O_CREAT | O_APPEND);
if (0 > FileID_s32)
{
CI_LocalPrintf ("Error open %s - %d - %s\r\n", Filename_pu8, fs_g_status, IBN_FileSystemErrorText (fs_g_status));
return (FALSE);
}
Ret_s32 = write (FileID_s32, Text_pu8, strlen ((char *) Text_pu8));
Ret_s32 = close (FileID_s32);
if (-1 == Ret_s32)
{
CI_LocalPrintf ("Close = %d \r\n", Ret_s32);
}
return (TRUE);
}
/*******************************************************************************
FileIO_AppendBin_u8
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
u8 FileIO_AppendBin_u8 (u8 * Filename_pu8, u8 * Bin_pu8, u16 Count_u16)
{
s32 FileID_s32;
s32 Ret_s32;
FileID_s32 = open ((char *) Filename_pu8, O_CREAT | O_APPEND);
if (0 > FileID_s32)
{
CI_LocalPrintf ("Error open %s - %d - %s\r\n", Filename_pu8, fs_g_status, IBN_FileSystemErrorText (fs_g_status));
return (FALSE);
}
Ret_s32 = write (FileID_s32, Bin_pu8, Count_u16);
Ret_s32 = close (FileID_s32);
if (-1 == Ret_s32)
{
CI_LocalPrintf ("Close = %d \r\n", Ret_s32);
}
return (TRUE);
}
/*******************************************************************************
FileIO_SaveAppImage_u8
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
#define FILEIO_STATUS_DIR "status"
#define FILEIO_IMAGE_FILE "firmware.bin"
#define FILEIO_IMAGE_START 0x80000000
#define FILEIO_IMAGE_SIZE (498*512) // 0x3E400 // Don't save the hidden volume and OTP data area below - 0x40000 - // 256 k
#define FILEIO_IMAGE_BLOCKSIZE 4096
u8 FileIO_SaveAppImage_u8 (void)
{
u16 i;
s32 FileID_s32;
s32 Ret_s32;
// Init uncrypted file io
SD_UncryptedFileIO_Init_u8 ();
// Delete possible old file
FAI_DeleteFile ((u8 *) FILEIO_IMAGE_FILE);
// Open File
FileID_s32 = open ((char *) FILEIO_IMAGE_FILE, O_CREAT | O_APPEND);
if (0 > FileID_s32)
{
CI_LocalPrintf ("Error open %s - %d - %s\r\n", FILEIO_IMAGE_FILE, fs_g_status, IBN_FileSystemErrorText (fs_g_status));
return (FALSE);
}
// Write data
for (i = 0; i < (FILEIO_IMAGE_SIZE / FILEIO_IMAGE_BLOCKSIZE); i++)
{
CI_LocalPrintf ("Write block = %3d\r\n", i);
Ret_s32 = write (FileID_s32, (u8 *) (FILEIO_IMAGE_START + i * FILEIO_IMAGE_BLOCKSIZE), FILEIO_IMAGE_BLOCKSIZE);
}
// Close file
Ret_s32 = close (FileID_s32);
if (-1 == Ret_s32)
{
CI_LocalPrintf ("Close = %d \r\n", Ret_s32);
}
// Flush and close the file
file_flush ();
return (TRUE);
}
/*******************************************************************************
FileIO_SaveAppImage_u8
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
/*
#define FILEIO_STATUS_DIR "status" #define FILEIO_IMAGE_FILE "status\\app.bin" #define FILEIO_IMAGE_START 0x80000000 #define FILEIO_IMAGE_SIZE
(498*512) // 0x3E400 // Don't save the hidden volume and OTP data area below - 0x40000 - // 256 k #define FILEIO_IMAGE_BLOCKSIZE 4096
u8 FileIO_SaveAppImage_u8_old (void) { u16 i; s32 FileID_s32; s32 Ret_s32;
// Init uncrypted file io SD_UncryptedFileIO_Init_u8 ();
// Change dir FileIO_CdDir_u8 ((u8*)FILEIO_STATUS_DIR);
// Delete possible old file FAI_DeleteFile ((u8*)FILEIO_IMAGE_FILE);
// Open File FileID_s32 = open ((char*)FILEIO_IMAGE_FILE,O_CREAT|O_APPEND); if (0 > FileID_s32) { CI_LocalPrintf ("Error open %s - %d -
%s\r\n",FILEIO_IMAGE_FILE,fs_g_status, IBN_FileSystemErrorText(fs_g_status)); return (FALSE); }
// Write data for (i=0;i<(FILEIO_IMAGE_SIZE/FILEIO_IMAGE_BLOCKSIZE);i++) { CI_LocalPrintf ("Write block = %3d\r\n",i); Ret_s32 = write
(FileID_s32,(u8*)(FILEIO_IMAGE_START + i * FILEIO_IMAGE_BLOCKSIZE),FILEIO_IMAGE_BLOCKSIZE); }
// Close file Ret_s32 = close (FileID_s32); if (-1 == Ret_s32) { CI_LocalPrintf ("Close = %d \r\n",Ret_s32); }
// Flush and close the file file_flush ();
// Inform host // FileIO_UpdateToHost_v ();
return (TRUE); } */
/*******************************************************************************
InitStatusFiles_u8
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
#define FILEIO_STATUS_FILE "status\\status.txt"
#define FILEIO_STATUS_TEXT_START "Welcome to Nitrokey"
u8 InitStatusFiles_u8 (void)
{
// Init uncrypted file io
SD_UncryptedFileIO_Init_u8 ();
// Change dir
FileIO_CdDir_u8 ((u8 *) FILEIO_STATUS_DIR);
// Write test
FileIO_AppendText_u8 ((u8 *) FILEIO_STATUS_FILE, (u8 *) FILEIO_STATUS_TEXT_START);
// Flush and close the file
file_flush ();
// Inform host
FileIO_UpdateToHost_v ();
return (TRUE);
}
#define FILEIO_DEBUG_FILE "debug.txt"
u8 WriteStrToDebugFile (u8 *String_pu8)
{
SD_UncryptedFileIO_Init_u8 ();
FileIO_AppendText_u8((u8 *) FILEIO_DEBUG_FILE, String_pu8);
file_flush();
return (TRUE);
}
/*******************************************************************************
IBN_FileIo_Tests
Reviews
Date Reviewer Info
16.08.13 RB First review
*******************************************************************************/
void IBN_FileIo_Tests (u8 nParamsGet_u8, u8 CMD_u8, u32 Param_u32, u32 Param_1_u32, u32 Param_2_u32)
{
if (0 == nParamsGet_u8)
{
CI_LocalPrintf ("File test functions\r\n");
CI_LocalPrintf ("0 Init SD interface\r\n");
CI_LocalPrintf ("1 Init status files\r\n");
CI_LocalPrintf ("2 Save app image\r\n");
CI_LocalPrintf ("\r\n");
return;
}
switch (CMD_u8)
{
case 0:
sd_mmc_mci_resources_init ();
break;
case 1:
InitStatusFiles_u8 ();
break;
case 2:
FileIO_SaveAppImage_u8 ();
break;
case 3:
FileIO_CdDir_u8 ((u8 *) FILEIO_STATUS_DIR);
break;
case 4:
FAI_mkdir ((u8 *) FILEIO_STATUS_DIR);
break;
}
}