summaryrefslogtreecommitdiff
path: root/fw/fe310/test/fs.c
blob: 0b3d4dcf2c171e8df958b510187c673818cd7d52 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <eos.h>
#include <dev/sdc_crypto.h>
#include <eve/eve.h>
#include <eve/eve_kbd.h>
#include <eve/eve_font.h>

#include <eve/screen/window.h>
#include <eve/screen/page.h>
#include <eve/screen/form.h>

#include <aes/aes.h>
#include <ff.h>

#include "fs.h"

FATFS fs;

#define TEXT_SIZE   128
#define TEXT_FN     "test.txt"
#define KEY         "passwordpassword"

PARTITION VolToPart[FF_VOLUMES] = {
    {0, 1}      /* "0:" ==> 1st partition on the pd#0 */
};

static EOSSDCCrypto sdcc;
static AESCtx ctx_crypt;
static AESCtx ctx_essiv;

void app_fs(EVEWindow *window, EVEViewStack *stack) {
    EVEFormSpec spec[] = {
        {
            .label.title = "Text",
            .widget.type = EVE_WIDGET_TYPE_STR,
            .widget.tspec.str.str_size = TEXT_SIZE,
        },
    };
    EVEForm *form = eve_form_create(window, stack, spec, 1, NULL, NULL, app_fs_close);
    EVEStrWidget *text = (EVEStrWidget *)eve_page_widget(&form->p, 0);
    FIL f;
    FRESULT rv;

    eos_spi_select(EOS_SPI_DEV_SDC);
    rv = f_open(&f, TEXT_FN, FA_READ);
    printf("f_open:%d\n", rv);
    if (!rv) {
        UINT r;

        rv = f_read(&f, text->str, TEXT_SIZE-1, &r);
        printf("f_read:%d\n", rv);
        if (rv != FR_OK) r = 0;
        text->str[r] = '\0';
        f_close(&f);
    }
    eos_spi_select(EOS_SPI_DEV_EVE);
    eve_strw_update(text);
}

void app_fs_close(EVEForm *form) {
    EVEStrWidget *text = (EVEStrWidget *)eve_page_widget(&form->p, 0);
    FIL f;
    FRESULT rv;

    eos_spi_select(EOS_SPI_DEV_SDC);
    rv = f_open(&f, TEXT_FN, FA_WRITE | FA_CREATE_ALWAYS);
    printf("f_open:%d\n", rv);
    if (!rv) {
        UINT w;

        rv = f_write(&f, text->str, strlen(text->str), &w);
        printf("f_write:%d\n", rv);
        f_close(&f);
    }
    eos_spi_select(EOS_SPI_DEV_EVE);
    eve_form_destroy(form);
}

void app_fs_init(void) {
    FRESULT rv;

    eos_sdcc_init(&sdcc, KEY, &ctx_crypt, (eve_sdcc_init_t)aes_init, (eve_sdcc_crypt_t)aes_cbc_encrypt, (eve_sdcc_crypt_t)aes_cbc_decrypt, &ctx_essiv, (eve_sdcc_init_t)aes_init, (eve_sdcc_essiv_t)aes_ecb_encrypt);
    eos_spi_select(EOS_SPI_DEV_SDC);
    rv = f_mount(&fs, "", 1);
    printf("f_mount:%d\n", rv);
    if (rv == FR_NO_FILESYSTEM) {
        uint8_t w[FF_MAX_SS];
        LBA_t plist[] = {100, 0};

        rv = f_fdisk(0, plist, w);
        printf("f_fdisk:%d\n", rv);
        rv = f_mkfs("0:", 0, w, sizeof(w));
        printf("f_mkfs:%d\n", rv);
        rv = f_mount(&fs, "", 1);
        printf("f_mount:%d\n", rv);
    }

    if (rv == FR_OK) {
        FIL f;
        UINT w;
        char *msg = "PERA JE CAR!";

        rv = f_open(&f, TEXT_FN, FA_READ);
        printf("f_open:%d\n", rv);
        if (!rv) {
            UINT r;
            char _msg[TEXT_SIZE];

            memset(_msg, 0, sizeof(_msg));
            rv = f_read(&f, _msg, sizeof(_msg), &r);
            printf("f_read:%d %u %s\n", rv, r, _msg);
            f_close(&f);
        } else {
            rv = f_open(&f, TEXT_FN, FA_WRITE | FA_CREATE_ALWAYS);
            printf("f_open:%d\n", rv);
            rv = f_write(&f, msg, strlen(msg), &w);
            printf("f_write:%d\n", rv);
        }
        f_close(&f);

    }
    eos_spi_deselect();
}