| 1 |
/* AFDISK - Amiga RDB Fdisk for Linux |
| 2 |
* fdisk.c part - generic input output stuff |
| 3 |
* written in 1996 by Stefan Reinauer |
| 4 |
* |
| 5 |
* Copyright (C) 1996-1998 Stefan Reinauer |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2, or (at your option) |
| 10 |
* any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <unistd.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <malloc.h> |
| 26 |
#include <string.h> |
| 27 |
#include <getopt.h> |
| 28 |
|
| 29 |
#include <readline/readline.h> |
| 30 |
#include <readline/history.h> |
| 31 |
|
| 32 |
#include <asm/byteorder.h> |
| 33 |
|
| 34 |
#include <amiga/types.h> |
| 35 |
#include <amigastuff.h> |
| 36 |
#include <implemented.h> |
| 37 |
|
| 38 |
#define VERSION "0.04" |
| 39 |
#define RELDATE "98/11/03" |
| 40 |
|
| 41 |
char *testdevs[] = { "/dev/hda", "/dev/hdb", "/dev/hdc", "/dev/hdd", |
| 42 |
"/dev/sda", "/dev/sdb", "/dev/sdc", "/dev/sdd", |
| 43 |
"/dev/sde", "/dev/sdf", "/dev/sdg", "/dev/sdh", |
| 44 |
NULL }; |
| 45 |
|
| 46 |
char disk_device[256]="/dev/sda"; |
| 47 |
int list_only=0, get_dev=0; |
| 48 |
|
| 49 |
int atonum(char *s) |
| 50 |
{ |
| 51 |
char *d=s; |
| 52 |
int n; |
| 53 |
|
| 54 |
while (*s == ' ') /* Eat spaces */ |
| 55 |
s++; |
| 56 |
|
| 57 |
/* 0x is hex, 0 is octal, everything else is decimal. */ |
| 58 |
if (strncmp(s, "0x", 2) == 0 || strncmp(s, "0X", 2) == 0) |
| 59 |
sscanf(s + 2, "%x", &n); |
| 60 |
else if (s[0] == '0' && s[1]) |
| 61 |
sscanf(s + 1, "%o", &n); |
| 62 |
else { |
| 63 |
d=s; |
| 64 |
while (d[0]!=0) { |
| 65 |
if (!isdigit( ((int)(d[0])) )) |
| 66 |
return 0; |
| 67 |
d++; |
| 68 |
} |
| 69 |
sscanf(s, "%d", &n); |
| 70 |
} |
| 71 |
return n; |
| 72 |
} |
| 73 |
|
| 74 |
int partition(void) |
| 75 |
{ |
| 76 |
char *partnum; |
| 77 |
int i; |
| 78 |
|
| 79 |
partition_show(); |
| 80 |
|
| 81 |
partnum=readline("Partition number: "); |
| 82 |
if (partnum==NULL) exit(0); |
| 83 |
i=atonum(partnum); |
| 84 |
free(partnum); |
| 85 |
|
| 86 |
return i; |
| 87 |
} |
| 88 |
|
| 89 |
int override(void) |
| 90 |
{ |
| 91 |
char *ovr; |
| 92 |
int i=0; |
| 93 |
if (!list_only) { |
| 94 |
ovr=readline("Do you want to override this error? [y/N] "); |
| 95 |
if (ovr==NULL) exit(0); |
| 96 |
if (ovr[0]=='y' || ovr[0]=='Y') i=1; |
| 97 |
free(ovr); |
| 98 |
} else { |
| 99 |
fprintf (stderr,"Overriding last error.\n"); |
| 100 |
i=1; |
| 101 |
} |
| 102 |
|
| 103 |
return i; |
| 104 |
} |
| 105 |
|
| 106 |
int listknown(void) |
| 107 |
{ |
| 108 |
printf("Known Partition Types\n---------------------\n |
| 109 |
1. DOS\\0 Old Filesystem |
| 110 |
2. DOS\\1 Original FastFileSystem (FFS) |
| 111 |
3. DOS\\2 Old International Filesystem |
| 112 |
4. DOS\\3 International FastFileSystem |
| 113 |
5. DOS\\4 Old Filesystem with directory cache |
| 114 |
6. DOS\\6 FastFileSystem with directory cache |
| 115 |
7. UNI\\0 classic AT&T System-V filesystem |
| 116 |
8. UNI\\1 UNIX boot \"filesystem\" (dummy entry for Amiga OS's boot menu) |
| 117 |
9. UNI\\2 Berkeley filesystem for System V |
| 118 |
10. RESV Reserved (e.g. swap space) |
| 119 |
11. LNX\\0 Linux native |
| 120 |
12. SWP\\0 Linux swap\n\n"); |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
int menu(void) |
| 126 |
{ |
| 127 |
printf ("Command action |
| 128 |
a toggle bootable flag |
| 129 |
b change number of bootblocks |
| 130 |
c change boot priority |
| 131 |
d delete a partition |
| 132 |
e toggle nomount flag |
| 133 |
l list known partition types |
| 134 |
m print this menu |
| 135 |
n add a new partition |
| 136 |
p print the partition table |
| 137 |
q quit without saving changes |
| 138 |
t change a partition's DosType |
| 139 |
r reorganize/move RDB |
| 140 |
w write table to disk and exit\n\n"); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
int version(void) |
| 145 |
{ |
| 146 |
printf ("amiga-fdisk %s (%s) by Stefan Reinauer, <[email protected]>\n\n",VERSION,RELDATE); |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
ULONG gettype(void) |
| 151 |
{ |
| 152 |
char *dtype; |
| 153 |
ULONG hexval,ascii; |
| 154 |
|
| 155 |
listknown(); |
| 156 |
|
| 157 |
printf ("\n Choose a number of the upper list or\n enter a DosType of the format 0x444f5302\n\n"); |
| 158 |
dtype=readline("Enter DosType: "); |
| 159 |
if (dtype==NULL) exit (0); |
| 160 |
hexval=atonum(dtype); |
| 161 |
ascii=ntohl(*(ULONG *)dtype); |
| 162 |
free(dtype); |
| 163 |
|
| 164 |
switch (hexval) { |
| 165 |
case 0: return ascii; |
| 166 |
case 1: return 0x444f5300; |
| 167 |
case 2: return 0x444f5301; |
| 168 |
case 3: return 0x444f5302; |
| 169 |
case 4: return 0x444f5303; |
| 170 |
case 5: return 0x444f5304; |
| 171 |
case 6: return 0x444f5305; |
| 172 |
case 7: return 0x554e4900; |
| 173 |
case 8: return 0x554e4901; |
| 174 |
case 9: return 0x554e4902; |
| 175 |
case 10: return 0x52455356; |
| 176 |
case 11: return 0x4C4E5800; |
| 177 |
case 12: return 0x53575000; |
| 178 |
default: return (hexval); |
| 179 |
} |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
int main(int argc, char **argv) |
| 185 |
{ |
| 186 |
int c, count; |
| 187 |
char *entry, *optinput; |
| 188 |
|
| 189 |
/* Parsing all command line parameters */ |
| 190 |
|
| 191 |
while(((c = getopt(argc, argv, "-dvls:TViN:A:u:xC:H:S:fqLnRO:I:?")) != EOF)) |
| 192 |
switch(c) { |
| 193 |
case 1: |
| 194 |
get_dev=1; |
| 195 |
strcpy(disk_device, optarg); |
| 196 |
break; |
| 197 |
case 'v': |
| 198 |
version(); |
| 199 |
return 0; |
| 200 |
case '?': |
| 201 |
printf ("\nUsage: %s [-l] [-v] [-d] [-s /dev/hdxn] [/dev/hdx]\n",argv[0]); |
| 202 |
return 0; |
| 203 |
case 'l': |
| 204 |
list_only = 1; |
| 205 |
break; |
| 206 |
case 'T': |
| 207 |
version(); |
| 208 |
listknown(); |
| 209 |
return 0; |
| 210 |
default: |
| 211 |
count=0; |
| 212 |
while (functions[count].key!='!') { |
| 213 |
if (functions[count].key==c) { |
| 214 |
printf ("Option \"%c\" - \"%s\" - not yet implemented.\n",c, functions[count].desc); |
| 215 |
break; |
| 216 |
} |
| 217 |
count++; |
| 218 |
} |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/* switch to selected mode and look for valid device */ |
| 224 |
|
| 225 |
if (list_only) { |
| 226 |
if (get_dev) { |
| 227 |
if (get_rdb()!=-1) { |
| 228 |
rigiddisk_show(); |
| 229 |
partition_show(); |
| 230 |
printf ("\n"); |
| 231 |
} |
| 232 |
} else { |
| 233 |
count=0; |
| 234 |
while (testdevs[count] != NULL) { |
| 235 |
strcpy(disk_device, testdevs[count++]); |
| 236 |
|
| 237 |
if (get_rdb()!=-1) { |
| 238 |
rigiddisk_show(); |
| 239 |
partition_show(); |
| 240 |
printf ("\n"); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
return 0; |
| 245 |
|
| 246 |
} else { |
| 247 |
if (!get_dev) |
| 248 |
printf ("Using %s as default device!\n",disk_device); |
| 249 |
if (get_rdb()==-1) |
| 250 |
return -1; |
| 251 |
} |
| 252 |
|
| 253 |
printf ("\n"); |
| 254 |
|
| 255 |
/* Entering interactive mode */ |
| 256 |
|
| 257 |
for (;;) { |
| 258 |
|
| 259 |
#ifdef DEBUG |
| 260 |
pt(); |
| 261 |
#endif |
| 262 |
|
| 263 |
entry=readline( "Command (m for help): "); |
| 264 |
if (entry==NULL) { |
| 265 |
quitall(); |
| 266 |
return 0; |
| 267 |
} |
| 268 |
switch ((*entry)) { |
| 269 |
case 'q': |
| 270 |
case 'Q': |
| 271 |
quitall(); |
| 272 |
free(entry); |
| 273 |
return 0; |
| 274 |
case 'l': |
| 275 |
case 'L': |
| 276 |
listknown(); |
| 277 |
break; |
| 278 |
case 'p': |
| 279 |
case 'P': |
| 280 |
printf ("\n"); |
| 281 |
rigiddisk_show(); |
| 282 |
partition_show(); |
| 283 |
printf ("\n"); |
| 284 |
break; |
| 285 |
case 'a': |
| 286 |
case 'A': |
| 287 |
partition_togglebootable(partition()); |
| 288 |
break; |
| 289 |
case 'b': |
| 290 |
case 'B': |
| 291 |
count=partition(); |
| 292 |
optinput=readline("Number of bootblocks: "); |
| 293 |
if (optinput==NULL) return 0; |
| 294 |
partition_bootblk(count,atonum(optinput)); |
| 295 |
free(optinput); |
| 296 |
break; |
| 297 |
case 'c': |
| 298 |
case 'C': |
| 299 |
count=partition(); |
| 300 |
optinput=readline("New Boot Priority: "); |
| 301 |
if (optinput==NULL) return 0; |
| 302 |
partition_bootpri(count,atonum(optinput)); |
| 303 |
free(optinput); |
| 304 |
break; |
| 305 |
case 'd': |
| 306 |
case 'D': |
| 307 |
partition_delete(partition()); |
| 308 |
break; |
| 309 |
case 'e': |
| 310 |
case 'E': |
| 311 |
partition_togglenomount(partition()); |
| 312 |
break; |
| 313 |
case 'n': |
| 314 |
case 'N': |
| 315 |
if (room_for_partition()!=-1) { |
| 316 |
count=partition(); |
| 317 |
if (partition_new(count)!=-1) { |
| 318 |
optinput=readline("Low Cylinder: "); |
| 319 |
if (optinput==NULL) return 0; |
| 320 |
partition_locyl(count,atonum(optinput)); |
| 321 |
free(optinput); |
| 322 |
|
| 323 |
optinput=readline("High Cylinder: "); |
| 324 |
if (optinput==NULL) return 0; |
| 325 |
partition_hicyl(count,atonum(optinput)); |
| 326 |
free(optinput); |
| 327 |
} |
| 328 |
} |
| 329 |
break; |
| 330 |
case 't': |
| 331 |
case 'T': |
| 332 |
count=partition(); |
| 333 |
partition_dostype(count,gettype()); |
| 334 |
break; |
| 335 |
|
| 336 |
case 'w': |
| 337 |
case 'W': |
| 338 |
free(entry); |
| 339 |
rigiddisk_reorg(firstblock); |
| 340 |
rigiddisk_save(); |
| 341 |
return 0; |
| 342 |
case 'r': |
| 343 |
case 'R': |
| 344 |
printf ("\n"); |
| 345 |
rigiddisk_show(); |
| 346 |
printf ("\n"); |
| 347 |
optinput=readline(" RDB start at: "); |
| 348 |
if (optinput==NULL) return 0; |
| 349 |
rigiddisk_reorg(atonum(optinput)); |
| 350 |
free(optinput); |
| 351 |
break; |
| 352 |
case '\0': |
| 353 |
break; |
| 354 |
case 'm': |
| 355 |
case 'M': |
| 356 |
default: |
| 357 |
menu(); |
| 358 |
break; |
| 359 |
} |
| 360 |
|
| 361 |
free(entry); |
| 362 |
|
| 363 |
} |
| 364 |
} |
| 365 |
|