| 1 | /******************************************************************************
 | 
|---|
| 2 | 
 | 
|---|
| 3 |   lbatest.c - simple (dumb) test program for os2ahci
 | 
|---|
| 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.
 | 
|---|
| 7 | 
 | 
|---|
| 8 |   THe program was written to run under Linux, since OS/2 physical disk
 | 
|---|
| 9 |   access IOCTLs support CHS addressing, only.
 | 
|---|
| 10 | 
 | 
|---|
| 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.
 | 
|---|
| 14 | 
 | 
|---|
| 15 |   Author: Markus Thielen
 | 
|---|
| 16 | 
 | 
|---|
| 17 |   Compilation: gcc -o lbatest lbatest.c
 | 
|---|
| 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>
 | 
|---|
| 40 | #include <stdlib.h>
 | 
|---|
| 41 | #include <ctype.h>
 | 
|---|
| 42 | #include <string.h>
 | 
|---|
| 43 | #include <stdio.h>
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #define SECTOR_SIZE        512
 | 
|---|
| 46 | #define SECTORS_PER_WRITE  256
 | 
|---|
| 47 | 
 | 
|---|
| 48 | 
 | 
|---|
| 49 | /*--- function prototypes ---------------------------------------------------*/
 | 
|---|
| 50 | 
 | 
|---|
| 51 | void          usage            (void);
 | 
|---|
| 52 | 
 | 
|---|
| 53 | void          write_test       (char *dev);
 | 
|---|
| 54 | 
 | 
|---|
| 55 | /*--- global data -----------------------------------------------------------*/
 | 
|---|
| 56 | 
 | 
|---|
| 57 | 
 | 
|---|
| 58 | /*--- start of code ---------------------------------------------------------*/
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /******************************************************************************
 | 
|---|
| 61 |  * main()
 | 
|---|
| 62 |  */
 | 
|---|
| 63 | int main(int argc, char **argv)
 | 
|---|
| 64 | {
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   if (argc < 2) {
 | 
|---|
| 67 |     usage();
 | 
|---|
| 68 |     return -1;
 | 
|---|
| 69 |   }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   write_test(argv[1]);
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   return 0;
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | 
 | 
|---|
| 77 | 
 | 
|---|
| 78 | /******************************************************************************
 | 
|---|
| 79 |  * write_test() - write each sector's address to the sector itself
 | 
|---|
| 80 |  */
 | 
|---|
| 81 | void write_test(char *dev)
 | 
|---|
| 82 | {
 | 
|---|
| 83 |   unsigned long lba;
 | 
|---|
| 84 |   float gbf = 1024.0 * 1024.0 * 1024.0 / 512.0;
 | 
|---|
| 85 |   size_t i;
 | 
|---|
| 86 |   unsigned char *wbuf;
 | 
|---|
| 87 |   char buf[100];
 | 
|---|
| 88 |   size_t cbtake = SECTOR_SIZE * SECTORS_PER_WRITE;
 | 
|---|
| 89 |   size_t cb_written;
 | 
|---|
| 90 |   FILE *fp;
 | 
|---|
| 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"
 | 
|---|
| 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;
 | 
|---|
| 97 |   }
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   /* open drive */
 | 
|---|
| 100 |   if ((fp = fopen(dev, "wb")) == NULL) {
 | 
|---|
| 101 |     perror("Failed to open device");
 | 
|---|
| 102 |     return;
 | 
|---|
| 103 |   }
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   /* allocate big write buffer */
 | 
|---|
| 106 |   wbuf = calloc(SECTOR_SIZE, SECTORS_PER_WRITE);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   /* go... */
 | 
|---|
| 109 |   for (lba = 0; ; lba += SECTORS_PER_WRITE) {
 | 
|---|
| 110 | 
 | 
|---|
| 111 |     /* prepare buffer; set the first 4 bytes of each sector
 | 
|---|
| 112 |      * to its LBA */
 | 
|---|
| 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 */
 | 
|---|
| 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");
 | 
|---|
| 122 |       }
 | 
|---|
| 123 |       break;
 | 
|---|
| 124 |     }
 | 
|---|
| 125 | 
 | 
|---|
| 126 |     /* write progress */
 | 
|---|
| 127 |     printf("\r%uk sectors written (%0.02f GB)",
 | 
|---|
| 128 |            (size_t)(lba / 1000), (float)lba / gbf);
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   }
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   /* cleanup */
 | 
|---|
| 133 |   fclose(fp);
 | 
|---|
| 134 |   free(wbuf);
 | 
|---|
| 135 | 
 | 
|---|
| 136 | }
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /******************************************************************************
 | 
|---|
| 139 |  * usage() - print usage summary to STDOUT
 | 
|---|
| 140 |  */
 | 
|---|
| 141 | void usage(void)
 | 
|---|
| 142 | {
 | 
|---|
| 143 |   printf("lbatest for os2ahci.add\n"
 | 
|---|
| 144 |          "Usage:\n\n"
 | 
|---|
| 145 |          "lbatest <device>\n\n");
 | 
|---|
| 146 | }
 | 
|---|
| 147 | 
 | 
|---|