Skip to content

Commit

Permalink
Finish mkrom
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Jan 27, 2022
1 parent d67c4bd commit 8d686c2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,16 @@ images:
$(OBJCOPY) -O binary $(RELEASE_DIR)/earth.elf $(BUILD_DIR)/earth.bin
$(CC) $(BUILD_DIR)/mkrom.c -o $(BUILD_DIR)/mkrom
cd $(BUILD_DIR); ./mkrom
#cd $(BUILD_DIR); vivado_lab -nojournal -mode batch -source arty_board/write_cfgmem.tcl; rm *.log *.prm
#srec_info $(BUILD_DIR)/egos_bootROM.mcs -Intel
#rm $(BUILD_DIR)/earth.bin
rm $(BUILD_DIR)/earth.bin

loc:
cloc . --exclude-dir=$(BUILD_DIR)

clean:
rm -rf $(DEBUG_DIR) $(RELEASE_DIR)
rm -rf $(BUILD_DIR)/mkfs $(BUILD_DIR)/mkrom
rm $(BUILD_DIR)/disk.img $(BUILD_DIR)/egos_bootROM.*
rm $(BUILD_DIR)/earth.bin $(BUILD_DIR)/*.log
rm -rf $(BUILD_DIR)/disk.img $(BUILD_DIR)/egos_bootROM.*
rm -rf $(BUILD_DIR)/earth.bin $(BUILD_DIR)/*.log

EARTH_SRCS = earth/*.c earth/sd/*.c shared/*.c
EARTH_LAYOUT = -Tearth/layout.lds
Expand Down
58 changes: 49 additions & 9 deletions install/mkrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
/* Author: Yunhao Zhang
* Description: create the bootROM image file (egos_bootROM.mcs)
* the bootROM has 16MB
* the first 4MB is reserved for the FE310 processor
* the second 4MB is reserved for the earth layer binary
* the last 8MB is reserved for the disk image
* the first 4MB is reserved for the FE310 processor
* the next 4MB is reserved for the earth layer binary
* the next 6MB is reserved for the disk image
* the last 2MB is currently unused
* the output file is in Intel MCS-86 object format
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
Expand All @@ -19,30 +22,70 @@
char disk_file[] = "disk.img";
char earth_file[] = "earth.bin";
char fe310_file[] = "arty_board/fe310_arty.bit";
char output_file[]= "egos_bootROM.mcs";

char mem_fe310[4 * 1024 * 1024];
char mem_earth[4 * 1024 * 1024];
char mem_disk [8 * 1024 * 1024];
char mem_disk [6 * 1024 * 1024];

void load_fe310();
void load_earth();
void load_disk();
void write_mcs();
void write_mcs_section();
int fe310_size, earth_size, disk_size;

int main() {
load_fe310();
load_earth();
load_disk();
write_mcs();

return 0;
}

void write_mcs() {
freopen(output_file, "w", stdout);

write_mcs_section(mem_fe310, 0x00, fe310_size);
fprintf(stderr, "[INFO] FE310 wrote\n");
write_mcs_section(mem_earth, 0x40, earth_size);
fprintf(stderr, "[INFO] Earth wrote\n");
write_mcs_section(mem_disk, 0x80, disk_size);
fprintf(stderr, "[INFO] Disk image wrote\n");
printf(":00000001FF\n");

fclose(stdout);

fprintf(stderr, "[INFO] Finish making the bootROM image\n");
}

void write_mcs_section(char* mem, int base, int size) {
/* using a dummy checksum */
char chk = 0xff;

int ngroups = (size >> 16) + 1;
for (int i = 0; i < ngroups; i++) {
printf(":02000004%.4X%.2X\n", i + base, chk & 0xff);
for (int j = 0; j < 0x10000; j += 16) {
printf(":10%.4X00", j);
for (int k = 0; k < 16; k++)
printf("%.2X", mem[i * 0x10000 + j + k] & 0xff);
printf("%.2X\n", chk & 0xff);
if (i * 0x10000 + j + 16 >= size)
return;
}
}
}

void load_disk() {
struct stat st;
stat(disk_file, &st);
disk_size = (int)st.st_size;
printf("[INFO] Disk image file has 0x%x bytes\n", disk_size);
assert(disk_size <= 6 * 1024 * 1024);

freopen(earth_file, "r", stdin);
freopen(disk_file, "r", stdin);
read(0, mem_disk, disk_size);
fclose(stdin);
}
Expand All @@ -63,23 +106,21 @@ void load_fe310() {
struct stat st;
stat(fe310_file, &st);
int len = (int)st.st_size;
printf("[INFO] FE310 binary file has %d bytes\n", len);
//printf("[INFO] FE310 binary file has %d bytes\n", len);

/* load header */
freopen(fe310_file, "r", stdin);
int first, second;
first = getchar();
second = getchar();
int length = (first << 8) + second;
//printf("[INFO] The header has length %d bytes\n", length);
for (int i = 0, tmp; i < length; i++)
tmp = getchar();

/* load key length */
first = getchar();
second = getchar();
int key_len = (first << 8) + second;
//printf("[INFO] Keys have length %d byte\n", key_len);
assert(key_len == 1);

while (1) {
Expand All @@ -92,7 +133,6 @@ void load_fe310() {
length = (first << 8) + second;
for (int i = 0, tmp; i < length; i++)
tmp = getchar();
//printf("[INFO] Section %c has length %d\n", (char)key, length);
}

int third, fourth;
Expand Down

0 comments on commit 8d686c2

Please sign in to comment.