Skip to content

Commit

Permalink
Reformat using clang-format based on llvm code style
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamPearl committed Oct 17, 2021
1 parent 158baea commit 83eaace
Show file tree
Hide file tree
Showing 101 changed files with 2,287 additions and 2,314 deletions.
24 changes: 13 additions & 11 deletions external/src/mbr_builder.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Classical generic MBR
// https://en.wikipedia.org/wiki/Master_boot_record#PTE
#include <fuzzy/fs/mbr.h>
#include<stdio.h>
#include <stdio.h>

char DRIVE = 0x80;
char DRIVE_INACTIVE = 0x00;
// https://en.wikipedia.org/wiki/Partition_type
char PARTITION_TYPE = 0x07; // Stealing exFAT
char PARTITION_TYPE = 0x07; // Stealing exFAT

// https://en.wikipedia.org/wiki/Cylinder-head-sector
void lba_to_chs(int lba, char chs[3]) {
Expand All @@ -26,28 +26,30 @@ void lba_to_chs(int lba, char chs[3]) {

void write_boot_signature(FILE *out) {
fseek(out, 510, SEEK_SET);
char boot_signature[2]={0x55, 0xAA};
char boot_signature[2] = {0x55, 0xAA};
fwrite(boot_signature, 1, sizeof(boot_signature), out);
}

void write_partition_entry(FILE *out, int id, char drive, int lba, int sector_count) {
void write_partition_entry(FILE *out, int id, char drive, int lba,
int sector_count) {
struct PartitionEntry entry;
entry.drive = drive;
entry.partition_type = PARTITION_TYPE;
entry.lba = lba;
entry.sector_count = sector_count;
// TODO: write lba using lba_to_chs too
fseek(out, MBR_PARTITION_BEGIN+id*sizeof(struct PartitionEntry), SEEK_SET);
fseek(out, MBR_PARTITION_BEGIN + id * sizeof(struct PartitionEntry),
SEEK_SET);
fwrite(&entry, 1, sizeof(entry), out);
printf("Added partition entry %d at lba: %d sector_count: %d\n",
id, lba, sector_count);
printf("Added partition entry %d at lba: %d sector_count: %d\n", id, lba,
sector_count);
}

void write_partition(FILE *out, FILE *in, int lba) {
int count;
char buffer[1024];
rewind(in);
fseek(out, lba*512, SEEK_SET);
fseek(out, lba * 512, SEEK_SET);
while ((count = fread(buffer, sizeof(char), sizeof(buffer), in)) > 0) {
fwrite(buffer, sizeof(char), count, out);
}
Expand Down Expand Up @@ -75,17 +77,17 @@ int main(int argc, char *argv[]) {

populate_outfile_using_image_prefix(out, image_prefix);

int lba, sector_count; // 4 bytes
int lba, sector_count; // 4 bytes
{
fseek(out, 0L, SEEK_END);
int file_size = ftell(out);
rewind(out);
lba=(file_size+511)/512;
lba = (file_size + 511) / 512;
}
{
fseek(part1, 0L, SEEK_END);
int file_size = ftell(part1);
sector_count=(file_size+511)/512;
sector_count = (file_size + 511) / 512;
}

write_boot_signature(out);
Expand Down
27 changes: 15 additions & 12 deletions external/src/mkfs_ffs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@
mkfs.ffs binary implementation for Linux.
*/

#include <dirent.h>
#include <fuzzy/fs/ffs.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>

#define FILEENTRY_LOCATION(file_id) (FS_FFS_FIRST_BLOCK_SIZE+FS_FFS_FILEENTRY_SIZE*(file_id))
#define FILEENTRY_LOCATION(file_id) \
(FS_FFS_FIRST_BLOCK_SIZE + FS_FFS_FILEENTRY_SIZE * (file_id))

void write_first_block(FILE *out) {
union FFSMetaData block;
strncpy(block.content.signature, FS_FFS_SIGNATURE, sizeof(block.content.signature));
strncpy(block.content.signature, FS_FFS_SIGNATURE,
sizeof(block.content.signature));

rewind(out);
fwrite(block.bytes, 1, sizeof(block.bytes), out);
}

void write_file(int file_id, FILE *outfile, int *outfile_nextdata_block,
const char *filename, FILE *srcfile,
int is_executable) {
const char *filename, FILE *srcfile, int is_executable) {
fseek(srcfile, 0L, SEEK_END);
int file_size = ftell(srcfile);
rewind(srcfile);
Expand All @@ -42,9 +43,9 @@ void write_file(int file_id, FILE *outfile, int *outfile_nextdata_block,
char buffer[512];
size_t bytes_read;
printf("Writting context at %d\n", ((*outfile_nextdata_block)));
fseek(outfile, (*outfile_nextdata_block)*FS_BLOCK_SIZE, SEEK_SET);
(*outfile_nextdata_block) += (file_size+FS_BLOCK_SIZE-1)/FS_BLOCK_SIZE;

fseek(outfile, (*outfile_nextdata_block) * FS_BLOCK_SIZE, SEEK_SET);
(*outfile_nextdata_block) +=
(file_size + FS_BLOCK_SIZE - 1) / FS_BLOCK_SIZE;

while ((bytes_read = fread(buffer, 1, sizeof(buffer), srcfile)) > 0) {
fwrite(buffer, 1, bytes_read, outfile);
Expand All @@ -60,7 +61,6 @@ void write_nofile(int file_id, FILE *out) {
fwrite(entry.bytes, 1, sizeof(entry.bytes), out);
}


int create_partition(char *src_dir, char *out_filepath) {
printf("dir:%s partition: %s\n", src_dir, out_filepath);
FILE *src = opendir(src_dir);
Expand All @@ -85,19 +85,22 @@ int create_partition(char *src_dir, char *out_filepath) {
strncat(buffer_filename, "/", sizeof(buffer_filename));
strncat(buffer_filename, de->d_name, sizeof(buffer_filename));
if (file_id >= FS_FFS_FILEENTRY_COUNT) {
fprintf("reached max supported files, ignoring file '%s'", buffer_filename);
fprintf("reached max supported files, ignoring file '%s'",
buffer_filename);
continue;
}
struct stat file_stat;
stat(buffer_filename, &file_stat);
if (!S_ISREG(file_stat.st_mode)) {
fprintf("skipping non-regular file '%s': %d", buffer_filename, file_stat.st_mode);
fprintf("skipping non-regular file '%s': %d", buffer_filename,
file_stat.st_mode);
continue;
}
int is_executable = file_stat.st_mode & S_IXUSR;

FILE *file_src = fopen(buffer_filename, "rb");
write_file(file_id++, out, &outfile_nextdata_block, de->d_name, file_src, is_executable);
write_file(file_id++, out, &outfile_nextdata_block, de->d_name,
file_src, is_executable);
fclose(file_src);
}
while (file_id < FS_FFS_FILEENTRY_COUNT) {
Expand Down
2 changes: 1 addition & 1 deletion include/fuzzy/drivers/pic/pic.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <stddef.h>

#define PIC_PIT_FREQ 1193182 // hz
#define PIC_PIT_FREQ 1193182 // hz
#define PIC_PIT_MAX_COUNTER 0xFFFF

#define PIC_IRQ_PIT 0
Expand Down
43 changes: 19 additions & 24 deletions include/fuzzy/drivers/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,32 @@

#include <stddef.h>

#define PORT_PIC1_CMD 0x20
#define PORT_PIC1_DATA 0x21
#define PORT_PIC2_CMD 0xA0
#define PORT_PIC2_DATA 0xA1
#define PORT_PIC1_CMD 0x20
#define PORT_PIC1_DATA 0x21
#define PORT_PIC2_CMD 0xA0
#define PORT_PIC2_DATA 0xA1

#define PORT_PIT_DATA0 0x40
#define PORT_PIT_DATA1 0x41
#define PORT_PIT_DATA2 0x42
#define PORT_PIT_CMD 0x43

#define PORT_PS2_DATA 0x60
#define PORT_PS2_CMD 0x64
#define PORT_PS2_STATUS 0x64
#define PORT_PIT_DATA0 0x40
#define PORT_PIT_DATA1 0x41
#define PORT_PIT_DATA2 0x42
#define PORT_PIT_CMD 0x43

#define PORT_PS2_DATA 0x60
#define PORT_PS2_CMD 0x64
#define PORT_PS2_STATUS 0x64

static inline void outb(uint16_t port, uint8_t data) {
__asm__ volatile(
"outb %0, %1 \n"
: /* output */
: "a" (data), "ir" (port) /* input */
:
);
__asm__ volatile("outb %0, %1 \n"
: /* output */
: "a"(data), "ir"(port) /* input */
:);
}

static inline uint8_t inputb(uint16_t port) {
uint8_t data;
__asm__ volatile(
"inb %1, %0 \n"
: "=a" (data) /* output */
: "ir" (port) /* input */
:
);
__asm__ volatile("inb %1, %0 \n"
: "=a"(data) /* output */
: "ir"(port) /* input */
:);
return data;
}
1 change: 0 additions & 1 deletion include/fuzzy/drivers/ps2/keyboard.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once


void ps2_keyboard_init();

char ps2_keyboard_get_key_pressed_blocking();
Expand Down
1 change: 0 additions & 1 deletion include/fuzzy/drivers/ps2/ps2.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ void ps2_controller_wait_for_full_output();
void interrupt_register_0x21_0x2C_irq1_ir12_keyboard_mouse();
void irq1_handler();
void irq12_handler();

32 changes: 13 additions & 19 deletions include/fuzzy/fs/ffs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ data...
Number of file which can be stored: 64
*/

#include <stdint.h>
#include <stddef.h>
#include <stdint.h>

#define FS_FFS_FILENAME_LIMIT 100 // including NULL, same as dirent.h
#define FFS_UNIQUE_PARITION_ID 0 // only paritition 0 is supported for now
#define FS_FFS_FILENAME_LIMIT 100 // including NULL, same as dirent.h
#define FFS_UNIQUE_PARITION_ID 0 // only paritition 0 is supported for now

enum FFSFileFlagMask {
FFS_FILE_FLAG_EXECUTABLE = 1 << 0
};
enum FFSFileFlagMask { FFS_FILE_FLAG_EXECUTABLE = 1 << 0 };

union FFSMetaData {
struct {
Expand All @@ -34,7 +32,7 @@ union FFSMetaData {

union FFSFileEntry {
struct {
int32_t start_block_id; // 0 implies no file.
int32_t start_block_id; // 0 implies no file.
int32_t filesize;
char filename[FS_FFS_FILENAME_LIMIT];
uint32_t flags;
Expand All @@ -47,24 +45,20 @@ union FFSFileEntry {
#define FS_FFS_FILEENTRY_COUNT 128

#define FS_BLOCK_SIZE 512
#define FS_FFS_BLOCK_DATA_START ((FS_FFS_FIRST_BLOCK_SIZE+FS_FFS_FILEENTRY_SIZE*FS_FFS_FILEENTRY_COUNT)/FS_BLOCK_SIZE)

#define FS_FFS_SIGNATURE "__FuzzyOS__FFS__" // 16 chars
#define FS_FFS_BLOCK_DATA_START \
((FS_FFS_FIRST_BLOCK_SIZE + \
FS_FFS_FILEENTRY_SIZE * FS_FFS_FILEENTRY_COUNT) / \
FS_BLOCK_SIZE)

#define FS_FFS_SIGNATURE "__FuzzyOS__FFS__" // 16 chars

int resolve_abs_lba(int parition_id, int partition_relative_lba);

int partition_read_block(int block_index, void *wr_buffer);

int verify_partition(int partition_id);

int fetch_file_entry(
int partition_id,
int entry_id,
union FFSFileEntry *entry);
int fetch_file_entry(int partition_id, int entry_id, union FFSFileEntry *entry);

int fetch_file_content(
const int partition_id,
const union FFSFileEntry *entry,
char buffer[FS_BLOCK_SIZE],
const int file_block_id);
int fetch_file_content(const int partition_id, const union FFSFileEntry *entry,
char buffer[FS_BLOCK_SIZE], const int file_block_id);
4 changes: 2 additions & 2 deletions include/fuzzy/fs/mbr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ struct PartitionEntry {
char start_chs[3];
char partition_type;
char end_chs[3];
int lba; // 4 bytes
int sector_count; // 4 bytes
int lba; // 4 bytes
int sector_count; // 4 bytes
};
21 changes: 9 additions & 12 deletions include/fuzzy/kernel/interrupts/interrupts.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
* [32:48) ; [0x20:0x30) ; IRQ0-15
* 80 ; 0x32 ; syscall
*/
#define IDT_SIZE 256
#define IDT_IRQ_OFFSET 0x20
#define IDT_SIZE 256
#define IDT_IRQ_OFFSET 0x20

// Used by /usr/lib/process.c
#define IDT_IRQ0_PIC (IDT_IRQ_OFFSET+0)
#define IDT_IRQ1_KEYBOARD (IDT_IRQ_OFFSET+1)
#define IDT_IRQ12_MOUSE (IDT_IRQ_OFFSET+12)
#define IDT_IRQ0_PIC (IDT_IRQ_OFFSET + 0)
#define IDT_IRQ1_KEYBOARD (IDT_IRQ_OFFSET + 1)
#define IDT_IRQ12_MOUSE (IDT_IRQ_OFFSET + 12)
// Used by /usr/lib/sys/syscall.asm
#define IDT_SYSCALL 0x32
#define IDT_SYSCALL 0x32


extern void populate_idt_entry_32bit(int id,
unsigned int address,
unsigned char dpl, // 2-bit
int is_trap
);
extern void populate_idt_entry_32bit(int id, unsigned int address,
unsigned char dpl, // 2-bit
int is_trap);
4 changes: 2 additions & 2 deletions include/fuzzy/kernel/panic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#define PANIC(err, message) panic((err), (message), __FILE__, __LINE__)

#define ASSERT(ok) (ok || panic(0, "Assert Failed: " #ok, __FILE__, __LINE__))
#define ASSERT(ok) (ok || panic(0, "Assert Failed: " #ok, __FILE__, __LINE__))

void panic_just_halt();
int panic(int err, const char *message, const char *src_file,
unsigned int line_number);
unsigned int line_number);
int panic_screen_init();
Loading

0 comments on commit 83eaace

Please sign in to comment.