Repository navigation
Expand file tree
/
Copy pathlef.c
More file actions
192 lines (160 loc) · 5.48 KB
/
Copy pathlef.c
File metadata and controls
192 lines (160 loc) · 5.48 KB
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
/* Copyright (C) 2026 HardenedLinux Community
* Nala Ginrut <roy@hardenedlinux.org>
* Animula is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* Animula 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 Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "lef.h"
void store_lef (lef_t lef, size_t offset)
{
size_t head_size = sizeof (struct LEF);
os_flash_write ((const void *)lef, offset, head_size);
os_flash_write ((const void *)lef->body, head_size, LEF_BODY_SIZE (lef));
}
void free_lef (lef_t lef)
{
os_free (lef->body);
lef->body = NULL;
os_free (lef);
lef = NULL;
}
lef_t load_lef_from_flash (size_t offset)
{
#if defined ANIMULA_ZEPHYR
lef_t lef = (lef_t)os_malloc (sizeof (struct LEF));
// convert char (*)[3] to char*
os_flash_read ((char *)(&lef->sig), 0, 3);
os_flash_read ((void *)&lef->msize, 7, 4);
os_flash_read ((void *)&lef->gsize, 11, 4);
os_flash_read ((void *)&lef->psize, 15, 4);
os_flash_read ((void *)&lef->csize, 19, 4);
u32_t size = LEF_BODY_SIZE (lef);
lef->body = (u8_t *)os_malloc (size);
os_flash_read ((void *)lef->body, 12, size);
u16_t sym_cnt = lef_get_u16 (0, lef);
u16_t symtab_size = lef_get_u16 (2, lef);
lef->symtab.cnt = sym_cnt;
lef->symtab.entry = lef->body + 4;
/* offset = sizeof(sym_cnt) + sizeof(symtab_size) + symtab_size */
u16_t symtab_offset = 4 + symtab_size;
lef->entry = lef_entry (symtab_offset, lef);
VM_DEBUG ("Done\n");
return lef;
#else
os_printk ("load_lef_from_uart doesn't support GNU/Linux platform!\n");
return NULL;
#endif
}
lef_t load_lef_from_uart ()
{
#if defined ANIMULA_ZEPHYR
lef_t lef = (lef_t)os_malloc (sizeof (struct LEF));
for (int i = 0; i < 3; i++)
lef->sig[i] = os_getchar ();
if (!LEF_VERIFY (lef))
{
uart_drop_rest_data ();
os_printk ("Wrong LEF file, please check it then re-upload!\n");
os_abort (-1);
}
for (int i = 0; i < 3; i++)
lef->ver[i] = os_getchar ();
lef->msize = uart_get_u32 ();
lef->gsize = uart_get_u32 ();
lef->psize = uart_get_u32 ();
lef->csize = uart_get_u32 ();
u32_t size = LEF_BODY_SIZE (lef);
lef->body = (u8_t *)os_malloc (size);
uint32_t last_index = 0;
uint32_t one_tenth_of_size = size / 10;
for (u32_t i = 0; i < size; i++)
{
u8_t ch = os_getchar ();
// print when every 16 bytes are sent or 10% of content
if ((i == size - 1) || (i - last_index >= (MAX (16, one_tenth_of_size))))
{
os_printk ("Upload: %d%%\n", ((i + 1) * 100) / size);
last_index = i;
}
lef->body[i] = ch;
}
os_printk ("Parsing LEF......\n");
u16_t sym_cnt = lef_get_u16 (0, lef);
u16_t symtab_size = lef_get_u16 (2, lef);
lef->symtab.cnt = sym_cnt;
lef->symtab.entry = lef->body + 4;
/* offset = sizeof(sym_cnt) + sizeof(symtab_size) + symtab_size */
u16_t symtab_offset = 4 + symtab_size;
lef->entry = lef_entry (symtab_offset, lef);
VM_DEBUG ("msize: %d\n", lef->msize);
VM_DEBUG ("gsize: %d\n", lef->gsize);
VM_DEBUG ("psize: %d\n", lef->psize);
VM_DEBUG ("csize: %d\n", lef->csize);
VM_DEBUG ("body: %p, prog: %p\n", lef->body, LEF_PROG (lef));
VM_DEBUG ("sym_cnt = %d\n", sym_cnt);
VM_DEBUG ("symtab_size = %d\n", symtab_size);
VM_DEBUG ("entry = %d\n", lef->entry);
VM_DEBUG ("Done\n");
return lef;
#else
os_printk ("load_lef_from_uart doesn't support GNU/Linux platform!\n");
return NULL;
#endif
}
lef_t load_lef_from_file (const char *filename)
{
lef_t lef = NULL;
#if defined(ANIMULA_LINUX) || defined(ANIMULA_ZEPHYR)
if (!file_exist (filename))
{
os_printk ("File \"%s\" doesn't exist!\n", filename);
os_abort (-1);
}
int fp;
lef = (lef_t)os_malloc (sizeof (struct LEF));
int fd = os_open_input_file (filename);
os_read (fd, lef->sig, 3);
if (!LEF_VERIFY (lef))
{
os_printk ("Wrong LEF file, please check it then re-upload!\n");
// FIXME: if load from zephyr, cannot exit directly
os_abort (-1);
}
os_read (fd, lef->ver, 3);
os_read_u32 (fd, &lef->msize);
os_read_u32 (fd, &lef->gsize);
os_read_u32 (fd, &lef->psize);
os_read_u32 (fd, &lef->csize);
u32_t size = LEF_BODY_SIZE (lef);
lef->body = (u8_t *)os_malloc (size);
os_read (fd, lef->body, size);
u16_t sym_cnt = lef_get_u16 (0, lef);
u16_t symtab_size = lef_get_u16 (2, lef);
lef->symtab.cnt = sym_cnt;
lef->symtab.entry = lef->body + 4;
/* offset = sizeof(sym_cnt) + sizeof(symtab_size) + symtab_size */
u16_t symtab_offset = 4 + symtab_size;
lef->entry = lef_entry (symtab_offset, lef);
VM_DEBUG ("msize: %d\n", lef->msize);
VM_DEBUG ("gsize: %d\n", lef->gsize);
VM_DEBUG ("psize: %d\n", lef->psize);
VM_DEBUG ("csize: %d\n", lef->csize);
VM_DEBUG ("body: %p, prog: %p\n", lef->body, LEF_PROG (lef));
VM_DEBUG ("sym_cnt = %d\n", sym_cnt);
VM_DEBUG ("symtab_size = %d\n", symtab_size);
VM_DEBUG ("entry = %d\n", lef->entry);
VM_DEBUG ("Done\n");
#else
os_printk ("The current platform %s doesn't support filesystem!\n",
get_platform_info ());
#endif
return lef;
}