source: trunk/src/lbatest/lbatest.c

Last change on this file was 52, checked in by markus, 15 years ago

nothing much

File size: 4.1 KB
RevLine 
[44]1/******************************************************************************
2
[49]3 lbatest.c - simple (dumb) test program for os2ahci
[44]4
5 Writes the actual LBA to each sector on a hard disk. The purpose is to
6 find out if the sector mapping of os2ahci.add is correct.
[52]7
[51]8 THe program was written to run under Linux, since OS/2 physical disk
9 access IOCTLs support CHS addressing, only.
[44]10
[51]11 The os2ahci.add driver provides a special operating mode that does nothing
12 but verifying that the tests drive's sector's are addressed in the correct
13 manner by verifying the sector numbers written under Linux.
[44]14
15 Author: Markus Thielen
16
[51]17 Compilation: gcc -o lbatest lbatest.c
[44]18
19 Copyright (c) 2010 by thi.guten Software development, www.thiguten.de
20
21 This program is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation; either version 2 of the License, or
24 (at your option) any later version.
25
26 This program is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
30
31 You should have received a copy of the GNU General Public License
32 along with this program; if not, write to the Free Software
33 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34
35******************************************************************************/
36
37/*---- includes -------------------------------------------------------------*/
38
39#include <stddef.h>
[49]40#include <stdlib.h>
[44]41#include <ctype.h>
42#include <string.h>
[51]43#include <stdio.h>
[44]44
[49]45#define SECTOR_SIZE 512
[51]46#define SECTORS_PER_WRITE 256
[44]47
[49]48
[44]49/*--- function prototypes ---------------------------------------------------*/
50
51void usage (void);
52
[51]53void write_test (char *dev);
[44]54
[49]55/*--- global data -----------------------------------------------------------*/
56
57
[44]58/*--- start of code ---------------------------------------------------------*/
59
60/******************************************************************************
61 * main()
62 */
63int main(int argc, char **argv)
64{
65
[51]66 if (argc < 2) {
[44]67 usage();
68 return -1;
69 }
70
[51]71 write_test(argv[1]);
[44]72
73 return 0;
74}
75
[50]76
77
78/******************************************************************************
[44]79 * write_test() - write each sector's address to the sector itself
80 */
[51]81void write_test(char *dev)
[44]82{
83 unsigned long lba;
84 float gbf = 1024.0 * 1024.0 * 1024.0 / 512.0;
[51]85 size_t i;
[49]86 unsigned char *wbuf;
87 char buf[100];
[51]88 size_t cbtake = SECTOR_SIZE * SECTORS_PER_WRITE;
89 size_t cb_written;
90 FILE *fp;
[44]91
92 /* ask for confirmation, we destroy the drive's contents */
93 printf("I'm going to destroy ALL DATA on drive %s now.\n"
[51]94 "type 'DESTROY' to continue, anything else to bail out.\n", dev);
95 if (fgets(buf, sizeof(buf), stdin) == NULL || strcmp(buf, "DESTROY\n")) {
96 return;
[44]97 }
98
99 /* open drive */
[51]100 if ((fp = fopen(dev, "wb")) == NULL) {
101 perror("Failed to open device");
102 return;
[44]103 }
104
[49]105 /* allocate big write buffer */
106 wbuf = calloc(SECTOR_SIZE, SECTORS_PER_WRITE);
107
[44]108 /* go... */
[49]109 for (lba = 0; ; lba += SECTORS_PER_WRITE) {
110
111 /* prepare buffer; set the first 4 bytes of each sector
112 * to its LBA */
[50]113 for (i = 0; i < SECTORS_PER_WRITE; i++) {
114 *((unsigned long *)(wbuf + i * SECTOR_SIZE)) = lba + i;
115 }
116
117 /* write buffer to disk */
[51]118 cb_written = fwrite(wbuf, 1, cbtake, fp);
119 if (cb_written < cbtake) {
120 if (!feof(fp)) {
121 perror("Failed to write to target device");
[49]122 }
[51]123 break;
[49]124 }
[52]125
[44]126 /* write progress */
[52]127 printf("\r%uk sectors written (%0.02f GB)",
[51]128 (size_t)(lba / 1000), (float)lba / gbf);
[52]129
[44]130 }
131
[51]132 /* cleanup */
133 fclose(fp);
[49]134 free(wbuf);
[44]135
136}
137
138/******************************************************************************
139 * usage() - print usage summary to STDOUT
140 */
141void usage(void)
142{
143 printf("lbatest for os2ahci.add\n"
144 "Usage:\n\n"
[51]145 "lbatest <device>\n\n");
[44]146}
[45]147
Note: See TracBrowser for help on using the repository browser.