Skip to content

Commit a025aa9

Browse files
committed
Add IDE/ATA support. Add numbers to keyboard.
ATA/IDE support is still bugged as hell.
1 parent 7d5ed49 commit a025aa9

File tree

15 files changed

+562
-21
lines changed

15 files changed

+562
-21
lines changed

drivers/ata.c

Lines changed: 396 additions & 2 deletions
Large diffs are not rendered by default.

drivers/device.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,22 @@ void device_init()
2121
_kill();
2222
}
2323

24+
void device_print_out()
25+
{
26+
for(int i = 0; i < lastid; i++)
27+
{
28+
//if(!devices[lastid]) return;
29+
kprintf("id: %d, unique: %d, %s, %s\n", i, devices[i].unique_id,
30+
devices[i].dev_type == DEVICE_CHAR ?"CHAR":"BLOCK", devices[i].name);
31+
}
32+
}
33+
2434
int device_add(device_t* dev)
2535
{
26-
devices[++lastid] = *dev;
36+
devices[lastid] = *dev;
2737
mprint("Registered Device %s (%d) as Device#%d\n", dev->name, dev->unique_id, lastid);
28-
return lastid;
38+
lastid++;
39+
return lastid-1;
2940
}
3041

3142
device_t *device_get_by_id(uint32_t id)

drivers/fdc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../include/pit.h"
88
#include "../include/device.h"
99
#include "../include/memory.h"
10+
#include "../include/levos.h"
1011

1112
#include <stdint.h>
1213

@@ -281,7 +282,7 @@ uint8_t* flpy_read_lba(int lba)
281282

282283
}
283284

284-
uint8_t flpy_read(uint8_t* buffer, uint32_t lba, uint32_t sectors)
285+
uint8_t flpy_read(uint8_t* buffer, uint32_t lba, uint32_t sectors, device_t *dev UNUSED)
285286
{
286287
if(!sectors) return 1;
287288
uint32_t sectors_read = 0;
@@ -336,7 +337,7 @@ uint8_t flpy_write_lba(uint8_t *buf, uint32_t lba)
336337

337338
}
338339

339-
uint8_t flpy_write(uint8_t *buffer, uint32_t lba, uint32_t sectors)
340+
uint8_t flpy_write(uint8_t *buffer, uint32_t lba, uint32_t sectors, device_t *dev UNUSED)
340341
{
341342
if(!sectors) return 0;
342343
uint32_t sectors_wrote = 0;

drivers/keyboard.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ enum KEYCODE {
7272
M_PRESSED = 0x32,
7373
M_RELEASED = 0xB2,
7474

75+
ZERO_PRESSED = 0x29,
76+
ONE_PRESSED = 0x2,
77+
NINE_PRESSED = 0xA,
78+
7579
POINT_PRESSED = 0x34,
7680
POINT_RELEASED = 0xB4,
7781

@@ -133,6 +137,7 @@ char keyboard_get_key()
133137
static char* _qwertzuiop = "qwertzuiop"; // 0x10-0x1c
134138
static char* _asdfghjkl = "asdfghjkl";
135139
static char* _yxcvbnm = "yxcvbnm";
140+
static char* _num = "123456789";
136141
uint8_t keyboard_to_ascii(uint8_t key)
137142
{
138143
//kprintf("key=0x%x\n", key);
@@ -141,6 +146,9 @@ uint8_t keyboard_to_ascii(uint8_t key)
141146
if(key == 0xE) return '\r';
142147
if(key == POINT_RELEASED) return '.';
143148
if(key == SLASH_RELEASED) return '/';
149+
if(key == ZERO_PRESSED) return '0';
150+
if(key >= ONE_PRESSED && key <= NINE_PRESSED)
151+
return _num[key - ONE_PRESSED];
144152
if(key >= 0x10 && key <= 0x1C)
145153
{
146154
return _qwertzuiop[key - 0x10];

fda.img

0 Bytes
Binary file not shown.

fs/ext2/ext2.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void ext2_read_block(uint8_t *buf, uint32_t block, device_t *dev, ext2_priv_data
1616
{
1717
uint32_t sectors_per_block = priv->sectors_per_block;
1818
if(!sectors_per_block) sectors_per_block = 1;
19-
dev->read(buf, block*sectors_per_block, sectors_per_block);
19+
dev->read(buf, block*sectors_per_block, sectors_per_block, dev);
2020

2121
}
2222
void ext2_read_inode(inode_t *inode_buf, uint32_t inode, device_t *dev, ext2_priv_data *priv)
@@ -212,11 +212,11 @@ uint8_t ext2_probe(device_t *dev)
212212
return 0;
213213
}
214214
uint8_t *buf = (uint8_t *)malloc(512);
215-
dev->read(buf, 2, 1);
215+
dev->read(buf, 2, 1, dev);
216216
superblock_t *sb = (superblock_t *)buf;
217217
if(sb->ext2_sig != EXT2_SIGNATURE)
218218
{
219-
kprintf("Invalid EXT2 signature!\n");
219+
kprintf("Invalid EXT2 signature, have: 0x%x!\n", sb->ext2_sig);
220220
return 0;
221221
}
222222
mprint("Valid EXT2 signature!\n");

hda.img

64 MB
Binary file not shown.

include/ata.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,91 @@
11
#ifndef __ATA_H_
22
#define __ATA_H_
33

4+
#include <stdint.h>
5+
6+
#define ATA_SR_BSY 0x80
7+
#define ATA_SR_DRDY 0x40
8+
#define ATA_SR_DF 0x20
9+
#define ATA_SR_DSC 0x10
10+
#define ATA_SR_DRQ 0x08
11+
#define ATA_SR_CORR 0x04
12+
#define ATA_SR_IDX 0x02
13+
#define ATA_SR_ERR 0x01
14+
15+
#define ATA_ER_BBK 0x80
16+
#define ATA_ER_UNC 0x40
17+
#define ATA_ER_MC 0x20
18+
#define ATA_ER_IDNF 0x10
19+
#define ATA_ER_MCR 0x08
20+
#define ATA_ER_ABRT 0x04
21+
#define ATA_ER_TK0NF 0x02
22+
#define ATA_ER_AMNF 0x01
23+
24+
#define ATA_CMD_READ_PIO 0x20
25+
#define ATA_CMD_READ_PIO_EXT 0x24
26+
#define ATA_CMD_READ_DMA 0xC8
27+
#define ATA_CMD_READ_DMA_EXT 0x25
28+
#define ATA_CMD_WRITE_PIO 0x30
29+
#define ATA_CMD_WRITE_PIO_EXT 0x34
30+
#define ATA_CMD_WRITE_DMA 0xCA
31+
#define ATA_CMD_WRITE_DMA_EXT 0x35
32+
#define ATA_CMD_CACHE_FLUSH 0xE7
33+
#define ATA_CMD_CACHE_FLUSH_EXT 0xEA
34+
#define ATA_CMD_PACKET 0xA0
35+
#define ATA_CMD_IDENTIFY_PACKET 0xA1
36+
#define ATA_CMD_IDENTIFY 0xEC
37+
38+
#define ATAPI_CMD_READ 0xA8
39+
#define ATAPI_CMD_EJECT 0x1B
40+
41+
#define ATA_IDENT_DEVICETYPE 0
42+
#define ATA_IDENT_CYLINDERS 2
43+
#define ATA_IDENT_HEADS 6
44+
#define ATA_IDENT_SECTORS 12
45+
#define ATA_IDENT_SERIAL 20
46+
#define ATA_IDENT_MODEL 54
47+
#define ATA_IDENT_CAPABILITIES 98
48+
#define ATA_IDENT_FIELDVALID 106
49+
#define ATA_IDENT_MAX_LBA 120
50+
#define ATA_IDENT_COMMANDSETS 164
51+
#define ATA_IDENT_MAX_LBA_EXT 200
52+
53+
#define IDE_ATA 0x00
54+
#define IDE_ATAPI 0x01
55+
56+
#define ATA_MASTER 0x00
57+
#define ATA_SLAVE 0x01
58+
59+
#define ATA_REG_DATA 0x00
60+
#define ATA_REG_ERROR 0x01
61+
#define ATA_REG_FEATURES 0x01
62+
#define ATA_REG_SECCOUNT0 0x02
63+
#define ATA_REG_LBA0 0x03
64+
#define ATA_REG_LBA1 0x04
65+
#define ATA_REG_LBA2 0x05
66+
#define ATA_REG_HDDEVSEL 0x06
67+
#define ATA_REG_COMMAND 0x07
68+
#define ATA_REG_STATUS 0x07
69+
#define ATA_REG_SECCOUNT1 0x08
70+
#define ATA_REG_LBA3 0x09
71+
#define ATA_REG_LBA4 0x0A
72+
#define ATA_REG_LBA5 0x0B
73+
#define ATA_REG_CONTROL 0x0C
74+
#define ATA_REG_ALTSTATUS 0x0C
75+
#define ATA_REG_DEVADDRESS 0x0D
76+
77+
// Channels:
78+
#define ATA_PRIMARY 0x00
79+
#define ATA_SECONDARY 0x01
80+
81+
// Directions:
82+
#define ATA_READ 0x00
83+
#define ATA_WRITE 0x013
84+
85+
typedef struct {
86+
uint8_t drive;
87+
} ide_private_data;
88+
489
extern void ata_init();
590

691
#endif

include/device.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ typedef struct __device_t {
1818
uint32_t unique_id;
1919
device_type dev_type;
2020
struct __fs_t *fs;
21-
uint8_t (*read)(uint8_t* buffer, uint32_t offset , uint32_t len);
22-
uint8_t (*write)(uint8_t *buffer, uint32_t offset, uint32_t len);
21+
uint8_t (*read)(uint8_t* buffer, uint32_t offset , uint32_t len, void* dev);
22+
uint8_t (*write)(uint8_t *buffer, uint32_t offset, uint32_t len, void* dev);
23+
void *priv;
2324
} device_t;
2425

26+
extern void device_print_out();
2527

2628
extern void device_init();
2729
extern int device_add(device_t* dev);

include/hal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
#define START(name, addr) addProcess(createProcess(name, (uint32_t)addr));
2626

27-
#define START_AND_WAIT(name, addr) int __pid_name = START(name, addr); \
28-
while(is_pid_running(__pid_name))schedule_noirq();
27+
#define START_AND_WAIT(NAME, ADDR) int ADDR ## _______pid = START(NAME, ADDR);while(is_pid_running(ADDR ## _______pid))schedule_noirq();
2928

3029
extern void hal_init();
3130

@@ -34,5 +33,9 @@ extern void send_eoi(uint8_t irq);
3433
extern void set_int(int i, uint32_t addr);
3534

3635
extern uint8_t inportb(uint16_t portid);
36+
extern uint16_t inportw(uint16_t portid);
3737
extern void outportb(uint16_t portid, uint8_t value);
38+
39+
#define insl(port, buffer, count) asm volatile("cld; rep; insl" :: "D" (buffer), "d" (port), "c" (count))
40+
3841
#endif

include/string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ typedef char* string;
99
extern size_t strlen(const string str);
1010
extern size_t strcmp(const string str1, const string str2);
1111

12+
extern void atoi(char *str, int* a);
13+
1214
extern size_t strcrl(string str, const char what, const char with);
1315
extern size_t str_begins_with(const string str, const string with);
1416
extern size_t str_backspace(string str, char c);

kernel/hal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ uint8_t inportb(uint16_t portid)
3030
asm volatile("inb %%dx, %%al":"=a"(ret):"d"(portid));
3131
return ret;
3232
}
33+
uint16_t inportw(uint16_t portid)
34+
{
35+
uint16_t ret;
36+
asm volatile("inw %%dx, %%ax":"=a"(ret):"d"(portid));
37+
return ret;
38+
}
3339
void outportb(uint16_t portid, uint8_t value)
3440
{
3541
asm volatile("outb %%al, %%dx": :"d" (portid), "a" (value));

lib/string.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ size_t strlen(const string str)
1313
return i;
1414
}
1515

16+
void atoi(char *str, int* a)
17+
{
18+
int k = 0;
19+
while(*str)
20+
{
21+
k = (k<<3)+(k<<1)+(*str)-'0';
22+
str++;
23+
}
24+
*a = k;
25+
}
26+
1627
size_t strcrl(string str, const char what, const char with)
1728
{
1829
size_t i = 0;

main.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void __cursor_updater()
115115
_kill();
116116
}
117117

118-
void test_device_read(uint8_t* buffer, uint32_t offset UNUSED, uint32_t len)
118+
void test_device_read(uint8_t* buffer, uint32_t offset UNUSED, uint32_t len, device_t *dev UNUSED)
119119
{
120120
len --;
121121
while(len--)
@@ -149,7 +149,7 @@ void create_test_device()
149149
testdev->name = "/dev/levex";
150150
testdev->unique_id = 0x1337;
151151
testdev->dev_type = DEVICE_CHAR;
152-
testdev->read = (uint8_t(*)(uint8_t*, uint32_t, uint32_t))test_device_read;
152+
testdev->read = (uint8_t(*)(uint8_t*, uint32_t, uint32_t, device_t*))test_device_read;
153153
levex_id = device_add(testdev);
154154
_kill();
155155
}
@@ -158,7 +158,7 @@ void __read()
158158
{
159159
uint8_t* buffer = (uint8_t*)malloc(32);
160160
device_t *testdev = device_get(levex_id);
161-
testdev->read(buffer, 0, 32);
161+
testdev->read(buffer, 0, 32, testdev);
162162
kprintf("%s\n", buffer);
163163
_kill();
164164
}
@@ -228,7 +228,6 @@ void kernel_main()
228228
panic("Reached end of main(), but tasking was not started.");
229229
for(;;);
230230
}
231-
232231
/* This function is ought to setup peripherials and such,
233232
* while also starting somekind of /bin/sh
234233
*/
@@ -246,7 +245,7 @@ void late_init()
246245
pid = START("testdev", create_test_device);
247246
pid = START("elf_init", elf_init);
248247
pid = START("rtc_init", rtc_init);
249-
pid = START("ata_init", ata_init);
248+
START_AND_WAIT("ata_init", ata_init);
250249
pid = START("fdc_init", fdc_init);
251250

252251
/* We now wait till all the late_inits have finished */
@@ -437,15 +436,33 @@ void _test()
437436
prompt_size = strlen(username) + strlen(hostname) + 4 + strlen(wd);
438437
}
439438
}
439+
if(strcmp(buffer, "devinfo") == 0)
440+
{
441+
device_print_out();
442+
}
440443
if(strcmp(buffer, "fl") == 0)
441444
{
442-
device_t *dev = device_get_by_id(19);
445+
if(!n || n == 1)
446+
{
447+
kprintf("FATAL: No id parameter, select root with devinfo\n");
448+
goto prompt;
449+
}
450+
char *arg = (char *)(buffer + strlen(buffer) + 1);
451+
int devid = 0;
452+
atoi(arg, &devid);
453+
kprintf("arg: %d\n", devid);
454+
device_t *dev = device_get(devid);
455+
if(!dev) {
456+
kprintf("Unable to locate medium!\n");
457+
goto prompt;
458+
}
443459
if(device_try_to_mount(dev, "/")) {
444460
kprintf("Mounted / on %s (%d) with %s\n", dev->name, dev->unique_id, dev->fs->name);
445461
root_mounted = 1;
446462
wd = (char *)malloc(512);
447463
memcpy(wd, "/", 2);
448464
prompt_size = strlen(username) + strlen(hostname) + 4 + strlen(wd);
465+
//device_try_to_mount(device_get_by_id(32), "/mnt/");
449466
START_AND_WAIT("proc_init", proc_init);
450467
}
451468
else kprintf("Unable to mount / on %s (%d)!\n", dev->name, dev->unique_id);
@@ -459,7 +476,7 @@ void _test()
459476
memset(write_buf, 0, 512);
460477
write_buf[0] = 0x37;
461478
write_buf[1] = 0x13;
462-
dev->write(write_buf, 0, 1);
479+
dev->write(write_buf, 0, 1, dev);
463480
kprintf("Wrote 0x1337 to first two bytes of the floppy.\n");
464481
}
465482
if(strcmp(buffer, "lev") == 0)

makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ CFLAGS=-std=gnu99 -ffreestanding -O2 -Wall -Wextra
77

88
mount:
99
echo "Mounting fda.img as /mnt/floppy..."
10+
sudo mount -o loop hda.img /mnt/hda
1011
sudo mount -o loop fda.img /mnt/floppy
1112
echo "Done."
1213

1314
start:
14-
qemu -kernel levos.bin -fda fda.img -monitor /dev/stdout
15+
qemu -kernel levos.bin -fda fda.img -hda hda.img -monitor /dev/stdout
1516
reset
1617

1718
assembly:

0 commit comments

Comments
 (0)