root / prex-0.9.0 / usr / test / ramdisk / ramdisk.c @ 03e9c04a
History | View | Annotate | Download (3.47 KB)
1 | 03e9c04a | Brad Neuman | /*
|
---|---|---|---|
2 | * Copyright (c) 2006, Kohsuke Ohtani
|
||
3 | * All rights reserved.
|
||
4 | *
|
||
5 | * Redistribution and use in source and binary forms, with or without
|
||
6 | * modification, are permitted provided that the following conditions
|
||
7 | * are met:
|
||
8 | * 1. Redistributions of source code must retain the above copyright
|
||
9 | * notice, this list of conditions and the following disclaimer.
|
||
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
||
11 | * notice, this list of conditions and the following disclaimer in the
|
||
12 | * documentation and/or other materials provided with the distribution.
|
||
13 | * 3. Neither the name of the author nor the names of any co-contributors
|
||
14 | * may be used to endorse or promote products derived from this software
|
||
15 | * without specific prior written permission.
|
||
16 | *
|
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||
27 | * SUCH DAMAGE.
|
||
28 | */
|
||
29 | |||
30 | /*
|
||
31 | * ramdisk.c - ramdisk driver test program.
|
||
32 | */
|
||
33 | |||
34 | #include <sys/prex.h> |
||
35 | #include <stdio.h> |
||
36 | #include <string.h> |
||
37 | #include <ctype.h> |
||
38 | |||
39 | static char test_msg[] = "Hello. This is a test message."; |
||
40 | |||
41 | int
|
||
42 | test_read(int sector)
|
||
43 | { |
||
44 | device_t ramdev; |
||
45 | size_t size; |
||
46 | int error, i, j;
|
||
47 | static unsigned char disk_buf[512]; |
||
48 | unsigned char ch; |
||
49 | |||
50 | printf("open ram0\n");
|
||
51 | error = device_open("ram0", 0, &ramdev); |
||
52 | if (error) {
|
||
53 | printf("open failed\n");
|
||
54 | return 0; |
||
55 | } |
||
56 | printf("opened\n");
|
||
57 | |||
58 | printf("ramdisk read: sector=%d buf=%x\n", sector, (u_int)disk_buf);
|
||
59 | size = 512;
|
||
60 | error = device_read(ramdev, disk_buf, &size, sector); |
||
61 | if (error) {
|
||
62 | printf("read failed\n");
|
||
63 | device_close(ramdev); |
||
64 | return 0; |
||
65 | } |
||
66 | printf("read comp: sector=%d buf=%x\n", sector, (u_int)disk_buf);
|
||
67 | |||
68 | for (i = 0; i < (512 / 16); i++) { |
||
69 | for (j = 0; j < 16; j++) |
||
70 | printf("%02x ", disk_buf[i * 16 + j]); |
||
71 | printf(" ");
|
||
72 | for (j = 0; j < 16; j++) { |
||
73 | ch = disk_buf[i * 16 + j];
|
||
74 | if (isprint(ch))
|
||
75 | putchar(ch); |
||
76 | else
|
||
77 | putchar('.');
|
||
78 | |||
79 | } |
||
80 | printf("\n");
|
||
81 | } |
||
82 | printf("\n");
|
||
83 | error = device_close(ramdev); |
||
84 | if (error)
|
||
85 | printf("close failed\n");
|
||
86 | |||
87 | return 0; |
||
88 | } |
||
89 | |||
90 | int
|
||
91 | test_write(int sector)
|
||
92 | { |
||
93 | device_t ramdev; |
||
94 | size_t size; |
||
95 | int error;
|
||
96 | static unsigned char disk_buf[512]; |
||
97 | |||
98 | printf("open ram0\n");
|
||
99 | error = device_open("ram0", 0, &ramdev); |
||
100 | if (error) {
|
||
101 | printf("open failed\n");
|
||
102 | return 0; |
||
103 | } |
||
104 | printf("opened\n");
|
||
105 | |||
106 | size = 512;
|
||
107 | error = device_read(ramdev, disk_buf, &size, sector); |
||
108 | if (error) {
|
||
109 | printf("read failed\n");
|
||
110 | device_close(ramdev); |
||
111 | return 0; |
||
112 | } |
||
113 | printf("read comp sector=%d\n", sector);
|
||
114 | |||
115 | strcpy((char *)disk_buf, test_msg);
|
||
116 | |||
117 | size = 512;
|
||
118 | error = device_write(ramdev, disk_buf, &size, sector); |
||
119 | if (error) {
|
||
120 | printf("write failed\n");
|
||
121 | device_close(ramdev); |
||
122 | return 0; |
||
123 | } |
||
124 | printf("write comp sector=%d\n", sector);
|
||
125 | |||
126 | error = device_close(ramdev); |
||
127 | if (error)
|
||
128 | printf("close failed\n");
|
||
129 | return 0; |
||
130 | } |
||
131 | |||
132 | int
|
||
133 | main(int argc, char *argv[]) |
||
134 | { |
||
135 | test_read(0);
|
||
136 | test_read(1);
|
||
137 | test_write(1);
|
||
138 | test_read(1);
|
||
139 | return 0; |
||
140 | } |