-
Notifications
You must be signed in to change notification settings - Fork 8
/
fdisk_hal_unix.c
154 lines (122 loc) · 2.96 KB
/
fdisk_hal_unix.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <strings.h>
#include <termios.h>
#include <time.h>
#include "fdisk_hal.h"
FILE *sdcard = NULL;
FILE *flash = NULL;
unsigned char get_random_byte(void)
{
return rand() % 256;
}
unsigned char sdcard_reset(void)
{
return 0;
}
void sdcard_select(unsigned char n)
{
return;
}
void sdcard_readsector(const uint32_t sector_number)
{
fseek(sdcard, sector_number * 512LL, SEEK_SET);
fread(sector_buffer, 512, 1, sdcard);
}
void sdcard_readspeed_test(void)
{
}
void mega65_fast(void)
{
}
void sdcard_map_sector_buffer(void)
{
}
uint32_t sdcard_getsize(void)
{
struct stat s;
if (!sdcard) {
fprintf(stderr, "SD card not open.\n");
exit(-1);
}
int r = fstat(fileno(sdcard), &s);
if (r) {
perror("stat");
exit(-1);
}
fprintf(stderr, "Size = $%08X sectors.\n", (unsigned int)(16000000000LL / 512LL));
// return s.st_size/512;
return 16000000000LL / 512;
}
void sdcard_open(void)
{
srand(time(NULL));
if (!getenv("SDCARDFILE")) {
fprintf(stderr, "ERROR: Environment variable 'SDCARDFILE' not found!\n");
fprintf(stderr, "- Please set it to either:\n"
" - a file (e.g., 'sdcard.bin')\n"
" - or your sd-card device (e.g., '/dev/sdb')\n"
"\n"
"- Also consider setting 'FLASHFILE' env-var to point to a .cor file\n");
exit(1);
}
sdcard = fopen(getenv("SDCARDFILE"), "r+");
if (!sdcard) {
fprintf(stderr, "Could not open '%s'...\n", getenv("SDCARDFILE"));
perror("fopen");
exit(-1);
}
}
uint32_t write_count = 0;
void sdcard_writesector(const uint32_t sector_number)
{
const uint8_t *buffer = sector_buffer;
fseek(sdcard, sector_number * 512LL, SEEK_SET);
fwrite(buffer, 512, 1, sdcard);
write_count++;
}
void sdcard_erase(const uint32_t first_sector, const uint32_t last_sector)
{
uint32_t n;
bzero(sector_buffer, 512);
fprintf(stderr, "Erasing sectors %d..%d\n", first_sector, last_sector);
for (n = first_sector; n <= last_sector; n++)
sdcard_writesector(n);
}
static int first_flash_read = 1;
void open_flash_file(void)
{
if (!getenv("FLASHFILE")) {
fprintf(stderr, "ERROR: Environment variable 'FLASHFILE' not found!\n");
exit(1);
}
fprintf(stderr, "FLASHFILE=%s\n", getenv("FLASHFILE"));
flash = fopen(getenv("FLASHFILE"), "rb+");
if (!flash) {
fprintf(stderr, "Could not open '%s'...\n", getenv("FLASHFILE"));
perror("fopen");
exit(-1);
}
}
void flash_read512bytes(const uint32_t byte_offset)
{
if (first_flash_read) {
first_flash_read = 0;
open_flash_file();
}
fseek(flash, byte_offset, SEEK_SET);
fread(sector_buffer, 512, 1, flash);
}
unsigned char mega65_getkey(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr(0, &oldattr);
newattr = oldattr;
newattr.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0, TCSANOW, &newattr);
ch = getchar();
tcsetattr(0, TCSANOW, &oldattr);
return (ch);
}