2ddeb1a2f29c0593b54208d7d62785edcf2786ce
[lunaix-os.git] / lunaix-os / kernel / demos / iotest.c
1 #include <lunaix/fctrl.h>
2 #include <lunaix/foptions.h>
3 #include <lunaix/proc.h>
4 #include <lunaix/syslog.h>
5
6 LOG_MODULE("IOTEST")
7
8 void
9 _iotest_main()
10 {
11     char test_sequence[] = "Once upon a time, in a magical land of Equestria. "
12                            "There were two regal sisters who ruled together "
13                            "and created harmony for all the land.";
14
15     int fd = open("/dev/sda", 0);  // bd0 设备 - 硬盘
16     int tty = open("/dev/tty", 0); // tty 设备 - 控制台
17
18     if (fd < 0 || tty < 0) {
19         kprintf(KERROR "fail to open (%d)\n", geterrno());
20         return;
21     }
22
23     // 移动指针至第二个逻辑扇区(LBA=1),并写入
24     lseek(fd, 1, FSEEK_SET);
25     write(fd, test_sequence, sizeof(test_sequence));
26
27     // 读出我们写的内容
28     char read_out[256];
29     lseek(fd, -1, FSEEK_CUR);
30     read(fd, read_out, sizeof(read_out));
31
32     write(tty, read_out, sizeof(read_out));
33
34     close(fd);
35     close(tty);
36
37     kprint_hex(read_out, sizeof(read_out));
38 }