Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/storage/ramdisk.c
1476 views
1
/*
2
* Ramdisk driver for Tegra X1
3
*
4
* Copyright (c) 2019-2021 CTCaer
5
*
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms and conditions of the GNU General Public License,
8
* version 2, as published by the Free Software Foundation.
9
*
10
* This program is distributed in the hope it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
* more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#include <string.h>
20
21
#include "ramdisk.h"
22
#include <libs/fatfs/diskio.h>
23
#include <libs/fatfs/ff.h>
24
#include <mem/heap.h>
25
#include <utils/types.h>
26
27
#include <memory_map.h>
28
29
static u32 disk_size = 0;
30
31
int ram_disk_init(void *ram_fs, u32 ramdisk_size)
32
{
33
int res = 0;
34
disk_size = ramdisk_size;
35
FATFS *ram_fatfs = (FATFS *)ram_fs;
36
37
// If ramdisk is not raw, format it.
38
if (ram_fs)
39
{
40
u8 *buf = malloc(0x400000);
41
42
// Set ramdisk size.
43
ramdisk_size >>= 9;
44
disk_set_info(DRIVE_RAM, SET_SECTOR_COUNT, &ramdisk_size);
45
46
// Unmount ramdisk.
47
f_mount(NULL, "ram:", 1);
48
49
// Format as exFAT w/ 32KB cluster with no MBR.
50
res = f_mkfs("ram:", FM_EXFAT | FM_SFD, RAMDISK_CLUSTER_SZ, buf, 0x400000);
51
52
// Mount ramdisk.
53
if (!res)
54
res = f_mount(ram_fatfs, "ram:", 1);
55
56
free(buf);
57
}
58
59
return res;
60
}
61
62
int ram_disk_read(u32 sector, u32 sector_count, void *buf)
63
{
64
u32 sector_off = RAM_DISK_ADDR + (sector << 9);
65
u32 bytes_count = sector_count << 9;
66
67
if ((sector_off - RAM_DISK_ADDR) > disk_size)
68
return 1;
69
70
memcpy(buf, (void *)sector_off, bytes_count);
71
72
return 0;
73
}
74
75
int ram_disk_write(u32 sector, u32 sector_count, const void *buf)
76
{
77
u32 sector_off = RAM_DISK_ADDR + (sector << 9);
78
u32 bytes_count = sector_count << 9;
79
80
if ((sector_off - RAM_DISK_ADDR) > disk_size)
81
return 1;
82
83
memcpy((void *)sector_off, buf, bytes_count);
84
85
return 0;
86
}
87
88