0cf7cba2e92a80dc37c221b7bdec6437eaf43369
[lunaix-os.git] / lunaix-os / kernel / demos / iotest.c
1 #include <lunaix/fctrl.h>
2 #include <lunaix/foptions.h>
3 #include <lunaix/lunistd.h>
4 #include <lunaix/proc.h>
5 #include <lunaix/syslog.h>
6
7 LOG_MODULE("IOTEST")
8
9 void
10 _iotest_main()
11 {
12     char test_sequence[] = "Once upon a time, in a magical land of Equestria. "
13                            "There were two regal sisters who ruled together "
14                            "and created harmony for all the land.";
15
16     int fd = open("/dev/sda", 0); // sda 设备 - 硬盘
17
18     // tty 设备 - 控制台,使用O_DIRECT打开,即所有IO绕过Lunaix内核的缓存机制
19     int tty = open("/dev/tty", FO_DIRECT);
20
21     if (fd < 0 || tty < 0) {
22         kprintf(KERROR "fail to open (%d)\n", geterrno());
23         return;
24     }
25
26     // 移动指针至512字节,在大多数情况下,这是第二个逻辑扇区的起始处
27     lseek(fd, 512, FSEEK_SET);
28     write(fd, test_sequence, sizeof(test_sequence));
29     lseek(fd, 521, FSEEK_SET);
30     write(fd, test_sequence, sizeof(test_sequence));
31
32     // 读出我们写的内容
33     char read_out[256];
34     lseek(fd, 512, FSEEK_SET);
35     read(fd, read_out, sizeof(read_out));
36
37     // 将读出的内容直接写入tty设备
38     write(tty, read_out, sizeof(read_out));
39     write(tty, "\n", 1);
40
41     close(fd);
42     close(tty);
43
44     kprint_hex(read_out, sizeof(read_out));
45 }