1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
|
/* amiga-fdisk - Amiga RDB fdisk for Linux
* amigastuff.c part - amiga specific stuff
* written in 1996 by Stefan Reinauer
*
* Copyright (C) 1996-1998 by Stefan Reinauer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
*/
#include <fcntl.h>
#include <unistd.h>
#include <linux/hdreg.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
# if 0
these include files are broken, see #252709
#include <asm/system.h>
#include <asm/byteorder.h>
#else
#include <netinet/in.h>
#endif
#include <amiga/types.h>
#include <amiga/filehandler.h>
#include <amiga/hardblocks.h>
#include <fdisk.h>
#define SECTOR_SIZE 512
#define MAXPARTS 16
#define BBB(pos) ((struct BadBlockBlock *)(pos))
#define PART(pos) ((struct PartitionBlock *)(pos))
#define FSHB(pos) ((struct FileSysHeaderBlock *)(pos))
#define LSEG(pos) ((struct LoadSegBlock *)(pos))
#define RDSK(pos) ((struct RigidDiskBlock *)(pos))
struct AmigaBlock {
ULONG amiga_ID; /* 4 character identifier */
ULONG amiga_SummedLongs; /* size of this checksummed structure */
LONG amiga_ChkSum; /* block checksum (longword sum to zero) */
};
struct RigidDiskBlock *rdb;
struct PartitionBlock *pa[MAXPARTS];
int parts, firstblock, lastblock, maxblock, minblock;
char *initsectors, *listsector;
char type[100]; /* Needed by DosType() */
int rigiddisk_new(int first);
char *get_block(int block)
{
/* This is a true quickhack. When we are in list only
* mode, we may not have swap and so we may not be able
* to hold the whole RDB data, so we do some stupid
* switching. This is slow and breaks the interactive mode.
* So, in interactive mode you still need to have all RDB data
* in memory at once
*/
int l;
if (list_only)
{
if ((l=open(disk_device,O_RDONLY))<0) {
if (get_dev) {
fprintf (stderr,"Cannot open device %s\n",disk_device);
return NULL;
}
}
if (lseek(l,(block*SECTOR_SIZE),SEEK_SET)<0) {
close(l);
fprintf (stderr,"Seek error occured while reading block %d.\n",block);
return NULL;
}
if (read(l,listsector,SECTOR_SIZE)!=SECTOR_SIZE) {
close(l);
fprintf (stderr,"Error occured while reading block %d.\n",block);
return NULL;
}
close(l);
return listsector;
}
return initsectors+(block*SECTOR_SIZE);
}
LONG checksum(struct AmigaBlock *ptr)
{
/* This function calculates the Checksum of one AmigaBlock. Trivial. */
int i, end;
LONG sum;
ULONG *pt=(ULONG *)ptr;
sum=htonl(pt[0]);
end=htonl(pt[1]);
if (end>SECTOR_SIZE)
end=SECTOR_SIZE;
for (i=1; i<end; i++) {
sum+=htonl(pt[i]);
}
return (LONG)sum;
}
int valid_blocktype(ULONG btype)
{
/* This is trivial, too. But as it appears quite often,
* I've put it into a function
*/
return (btype==IDNAME_RIGIDDISK ||
btype==IDNAME_BADBLOCK ||
btype==IDNAME_FILESYSHEADER ||
btype==IDNAME_LOADSEG ||
btype==IDNAME_PARTITION);
}
int valid_blocknum(int block)
{
return ( (block>=minblock && block<=maxblock) || (block == -1));
}
int sector_correct(struct AmigaBlock *ablock)
{
if (valid_blocktype(htonl(ablock->amiga_ID)))
{
ablock->amiga_ChkSum = ntohl( htonl(ablock->amiga_ChkSum) -
checksum((struct AmigaBlock *)ablock));
return 0;
}
return -1;
}
int check_lseg(int blk)
{
char *curr;
while (blk!=-1) {
if (blk>lastblock) lastblock=blk;
curr=get_block(blk);
if (htonl(LSEG(curr)->lsb_ID)!=IDNAME_LOADSEG) {
fprintf (stderr, "LoadSeg messed up at block #%d.\n",blk);
if (!override())
return -1;
LSEG(curr)->lsb_ID=ntohl(IDNAME_LOADSEG);
}
if (checksum((struct AmigaBlock *)curr)!=0) {
fprintf (stderr, "LoadSeg block #%d had wrong checksum. Fixed.\n",blk);
sector_correct((struct AmigaBlock *)curr);
}
blk=htonl(LSEG(curr)->lsb_Next);
if (!valid_blocknum(blk)) {
fprintf (stderr, "LoadSeg block not valid. Skipping other LoadSeg blocks.\n");
LSEG(curr)->lsb_Next=ntohl(-1); // Correct this.
break;
}
}
return 0;
}
int check_bbb(int blk)
{
char *curr;
while (blk!=-1) {
if (blk>lastblock) lastblock=blk;
curr=get_block(blk);
if (htonl(BBB(curr)->bbb_ID)!=IDNAME_BADBLOCK) {
fprintf (stderr, "BadBlockList messed up at block #%d.\n",blk);
return -1;
}
if (checksum((struct AmigaBlock *)curr)!=0) {
fprintf (stderr, "BadBlockList block #%d had wrong checksum. Fixed.\n",blk);
sector_correct((struct AmigaBlock *)curr);
}
blk=htonl(BBB(curr)->bbb_Next);
if (!valid_blocknum(blk)) {
fprintf (stderr, "BadBlockList block not valid. Skipping other BadBlockList blocks.\n");
BBB(curr)->bbb_Next=ntohl(-1); // Correct this.
break;
}
}
return 0;
}
int connectivity(void)
{
char *curr;
int block;
/* Check whether all checksums are ok and try to fix them
* Extract partition data.
*/
lastblock=firstblock;
/* Check BadBlockBlocks, LoadSegBlocks & FileSystemHeaderBlocks */
if (check_bbb(htonl(rdb->rdb_BadBlockList))==-1) return -1;
if (check_lseg(htonl(rdb->rdb_DriveInit))==-1) return -1;
block=htonl(rdb->rdb_FileSysHeaderList);
while (block!=-1) {
if (block>lastblock) lastblock=block;
curr=get_block(block);
if (htonl(FSHB(curr)->fhb_ID)!=IDNAME_FILESYSHEADER) {
fprintf (stderr, "FilesystemHeader messed up at block #%d.\n",block);
return -1;
}
if (checksum((struct AmigaBlock *)curr)!=0) {
fprintf (stderr, "FilesystemHeader block #%d had wrong checksum. Fixed.\n",block);
sector_correct((struct AmigaBlock *)curr);
}
if (check_lseg(htonl(FSHB(curr)->fhb_SegListBlocks))==-1) return -1;
if (list_only) /* Because we overwrote the buffer with check_lseg */
curr=get_block(block);
block=htonl(FSHB(curr)->fhb_Next);
}
/* Check Partition Blocks */
parts=0;
block=htonl(rdb->rdb_PartitionList);
while (block!=-1) {
if (block>lastblock) lastblock=block;
curr=get_block(block);
if (htonl(PART(curr)->pb_ID)!=IDNAME_PARTITION) {
fprintf (stderr, "PartitionEntry messed up at block #%d.\n",block);
return -1;
}
if (checksum((struct AmigaBlock *)curr)!=0) {
fprintf (stderr, "PartitionEntry block #%d had wrong checksum. Fixed.\n",block);
sector_correct((struct AmigaBlock *)curr);
}
if (!list_only)
pa[parts]=PART(curr);
parts++;
if (parts>MAXPARTS) {
fprintf (stderr, "Too many partitions(max=%d). Giving up.\n",MAXPARTS);
return -1;
}
block=htonl(PART(curr)->pb_Next);
}
/* Everything seems to be alright */
return 0;
}
int get_rdb(void)
{
/* Initial work.
* - Get RDB
* - Check size of all init blocks
* - get init sectors
*/
int f,i;
if ((rdb=RDSK(malloc(SECTOR_SIZE)))==NULL) {
fprintf (stderr,"Not enough memory for one sector (poor guy).\n");
return -1;
}
if ((f=open(disk_device,O_RDONLY))<0) {
if (!list_only || get_dev)
fprintf (stderr,"Cannot open device %s\n",disk_device);
free((char *)rdb);
return -1;
}
i=0;
firstblock=-1;
while ((i<RDB_LOCATION_LIMIT)&&(firstblock==-1)) {
if (lseek(f,(i*SECTOR_SIZE),SEEK_SET)<0) {
close(f);
free((char *)rdb);
fprintf (stderr,"Seek error occured while reading RDB.\n");
return -1;
}
if (read(f,(char *)rdb,SECTOR_SIZE)!=SECTOR_SIZE) {
close(f);
free((char *)rdb);
fprintf (stderr,"Error occured while reading RDB.\n");
return -1;
}
if (htonl(rdb->rdb_ID)==IDNAME_RIGIDDISK) {
firstblock=i;
if (checksum((struct AmigaBlock *)rdb)) {
sector_correct((struct AmigaBlock *)rdb);
fprintf(stderr,"RDB on block %d had bad checksum. Fixed.\n",i);
}
}
i++;
}
if (firstblock==-1) {
fprintf(stderr,"No valid RDB found... ");
close(f); free((char *)rdb);
rigiddisk_new(0);
return 0;
}
if ((i=htonl(rdb->rdb_RDBBlocksHi))==0) {
fprintf (stderr,"RDB unrepairable inconsistent.\n");
return -1;
}
if (!list_only) {
free ((char *)rdb);
if ((initsectors=malloc(i*SECTOR_SIZE))==NULL) {
fprintf (stderr,"Not enough memory for partition table.\n");
return -1;
}
if (lseek(f,0,SEEK_SET)<0) {
close(f); free(initsectors);
fprintf (stderr,"Seek error occured while reading partition table.\n");
return -1;
}
if (read(f,initsectors,i*SECTOR_SIZE)!=i*SECTOR_SIZE) {
close(f);
free(initsectors);
fprintf (stderr,"Error while reading partition table.\n");
return -1;
}
rdb=RDSK(initsectors+firstblock*SECTOR_SIZE);
} else {
if ((listsector=malloc(SECTOR_SIZE))==NULL) {
fprintf (stderr,"Not enough memory for one sector. Poor guy.\n");
return -1;
}
}
close (f);
maxblock=htonl(rdb->rdb_RDBBlocksHi);
minblock=htonl(rdb->rdb_RDBBlocksLo);
if (connectivity()==-1) {
free(initsectors); fprintf (stderr,"Could not solve RDB problems. Sorry.\n");
return -1;
}
/* Everything alright ? */
return 0;
}
int countblk(void)
{
int i,j=0;
struct AmigaBlock *curr;
// Implement: Checking whether found block is in chained list
for (i=0;i<=maxblock;i++) {
curr=(struct AmigaBlock *)get_block(i);
if (valid_blocktype(htonl(curr->amiga_ID))) j++;
}
#ifdef DEBUG
printf ("%d/%d blocks counted.\n",j,maxblock);
#endif
return j;
}
int findblk(void)
{
int i;
struct AmigaBlock *curr;
// Implement: Checking whether found block is in chained list
for (i=firstblock;i<=maxblock;i++) {
curr=(struct AmigaBlock *)get_block(i);
if (!valid_blocktype(htonl(curr->amiga_ID))) {
printf ("block %d is free.\n",i);
return i;
}
}
return -1;
}
int room_for_partition(void)
{
/* This function will never be called in list_only mode :-) */
int i;
if (parts==MAXPARTS) {
fprintf (stderr,"Too many partitions(max=%d).\n",MAXPARTS);
return -1;
}
if ((i=findblk())<minblock) {
fprintf (stderr,"No room in RDB area for new partition. Sorry. (minblock: %d) \n", minblock);
return -1;
}
return i;
}
int partition_new(int nr)
{
int i,j;
struct DosEnvec *de;
if ((nr<1)||(nr>parts+1)) {
fprintf (stderr,"Invalid partition number. existing: %d, tried: %d\n",parts,nr);
return -1;
}
if ((i=room_for_partition())==-1) {
fprintf (stderr,"Could not create new partition.\n");
return -1;
}
printf ("Creating new partition entry at block %d.\n",i);
nr--;
for (j=parts;j>nr;j--)
pa[j]=pa[j-1];
parts++;
if (i>lastblock) lastblock=i;
rdb->rdb_HighRDSKBlock=ntohl(lastblock);
pa[nr]=PART(get_block(i)); /* And we hope, that list_only will
* never call us :)
*/
memset(pa[nr],-1,SECTOR_SIZE);
pa[nr]->pb_ID = ntohl(IDNAME_PARTITION);
pa[nr]->pb_SummedLongs = ntohl(64);
pa[nr]->pb_HostID = rdb->rdb_HostID;
pa[nr]->pb_Flags = ntohl(0);
pa[nr]->pb_DevFlags = ntohl(0);
memcpy(pa[nr]->pb_DriveName,"\003dhx",4);
if (nr==0) {
pa[nr]->pb_Next=rdb->rdb_PartitionList;
rdb->rdb_PartitionList=ntohl(i);
} else {
pa[nr]->pb_Next=pa[nr-1]->pb_Next;
pa[nr-1]->pb_Next=ntohl(i);
}
de=(struct DosEnvec *)pa[nr]->pb_Environment;
de->de_TableSize = ntohl(19); /* 20 ? */
de->de_SizeBlock = ntohl(128);
de->de_SecOrg = ntohl(0);
de->de_Surfaces = ntohl(1);
/* On my old Seagate this was 1. If it does not work, try
* de->de_Surfaces=rdb->rdb_Heads */
de->de_SectorPerBlock = ntohl(1);
de->de_BlocksPerTrack = rdb->rdb_CylBlocks;
/* BlocksPerTrack should be rdb->rdb_sectors?! But CylBlocks
* seems to be better if a hd has more than 1 _LOGICAL_ surface
* (Does this ever happen? Correct me if I am wrong..
*/
de->de_Reserved = ntohl(2);
de->de_PreAlloc = ntohl(0);
de->de_Interleave = ntohl(0);
de->de_NumBuffers = ntohl(30);
de->de_BufMemType = ntohl(0);
de->de_MaxTransfer = ntohl(0x7fffffff);
de->de_Mask = ntohl(0xffffffff);
de->de_BootPri = ntohl(0);
de->de_DosType = ntohl(0x444f5301);
de->de_Baud = ntohl(0);
de->de_Control = ntohl(0);
de->de_BootBlocks = ntohl(0);
return 0;
}
int partition_delete (int nr)
{
if ((parts<nr)||(nr<1)) {
fprintf (stderr,"Could not delete partition %d.\n",nr);
return -1;
}
nr--;
if (nr==0) {
if (htonl(rdb->rdb_PartitionList)==(ULONG)lastblock)
lastblock--; /* Correct lastblock counter */
rdb->rdb_PartitionList=pa[0]->pb_Next;
} else {
if (htonl(pa[nr-1]->pb_Next)==(ULONG)lastblock)
lastblock--;
pa[nr-1]->pb_Next=pa[nr]->pb_Next;
}
while (nr<parts) {
pa[nr]=pa[nr+1];
nr++;
}
pa[parts] = NULL;
parts--;
return 0;
}
int partition_togglebootable(int nr)
{
if ((nr<1)||(nr>parts)) return -1;
nr--;
pa[nr]->pb_Flags=ntohl(htonl(pa[nr]->pb_Flags)^PBFF_BOOTABLE);
return 0;
}
int partition_togglenomount(int nr)
{
if ((nr<1)||(nr>parts)) return -1;
nr--;
pa[nr]->pb_Flags=ntohl(htonl(pa[nr]->pb_Flags)^PBFF_NOMOUNT);
return 0;
}
int partition_locyl(int nr,int cyl)
{
if ((nr<1)||(nr>parts)) return -1;
nr--;
((struct DosEnvec *)(pa[nr]->pb_Environment))->de_LowCyl=ntohl(cyl);
return 0;
}
int partition_hicyl(int nr,int cyl)
{
if ((nr<1)||(nr>parts)) return -1;
nr--;
((struct DosEnvec *)pa[nr]->pb_Environment)->de_HighCyl=ntohl(cyl);
return 0;
}
int partition_bootpri(int nr,int pri)
{
if ((nr<1)||(nr>parts)) return -1;
nr--;
((struct DosEnvec *)pa[nr]->pb_Environment)->de_BootPri=ntohl(pri);
return 0;
}
int partition_bootblk(int nr,int blks)
{
if ((nr<1)||(nr>parts)) return -1;
nr--;
((struct DosEnvec *)pa[nr]->pb_Environment)->de_BootBlocks=ntohl(blks);
return 0;
}
int partition_dostype(int nr, ULONG dostype)
{
if ((nr<1)||(nr>parts)) return -1;
nr--;
((struct DosEnvec *)pa[nr]->pb_Environment)->de_DosType=ntohl(dostype);
return 0;
}
int rigiddisk_correct(void)
{
/* Correct checksums of all Blocks */
int i,ts;
struct DosEnvec *de;
rdb->rdb_HighRDSKBlock=ntohl(lastblock);
/* Historical overhead from the very old days. The DosEnvec is a
dynamically growing table, and some fields are only present in newer
versions of AmigaDos. So if we decide to create acient looking RDB's,
we are ready for it. (If a partion is mounted under AmigaDos, unknown
fields should be ignored, and missing fields given a senseful default.)
NB, AmigaLilo depends on the presence of fields 19 and 20, which are
only supported by V37 or newer ROMs.
*/
for (i=firstblock;i<maxblock+1;i++)
sector_correct((struct AmigaBlock *)(get_block(i)));
connectivity();
for (i=0;i<parts;i++) {
de=(struct DosEnvec *)pa[i]->pb_Environment;
ts=htonl(de->de_TableSize);
if (ts<12) de->de_BufMemType=ntohl(0);
if (ts<13) de->de_MaxTransfer=ntohl(0x7fffffff);
if (ts<14) de->de_Mask=ntohl(0xffffffff);
if (ts<15) de->de_BootPri=ntohl(0);
if (ts<16) de->de_DosType=ntohl(0x444f5300);
if (ts<17) de->de_Baud=ntohl(0);
if (ts<18) de->de_Control=ntohl(0);
if (ts<19) {
de->de_BootBlocks=ntohl(0);
de->de_TableSize=ntohl(19);
}
sector_correct((struct AmigaBlock *)(pa[i]));
}
return 0;
}
int rigiddisk_new(int first)
{
struct hd_geometry geo={-1,};
long long hdsize;
int i,f;
if ((f=open(disk_device,O_RDONLY))<0) {
fprintf (stderr,"Cannot open device %s\n",disk_device);
return -1;
}
printf ("Creating new Rigid Disk Block\n");
if (ioctl(f,HDIO_GETGEO,&geo)!=0) {
printf("Can't get geometry data. Trying lseek/trivial mapping.\n");
hdsize=lseek(f,0,SEEK_END);
if (hdsize != -1) {
if (hdsize==0) {
printf("LSEEK: Could not lseek (Floppy?). Exit.\n");
exit(0);
}
/* XXXXXXX Fixme */
if ((hdsize%SECTOR_SIZE)!=0) {
printf("LSEEK: File length error (corrupt disk image?). Exit.\n");
exit(0);
}
geo.heads=1;
geo.sectors=1;
// geo.cylinders=hdsize/SECTOR_SIZE;
hdsize/=SECTOR_SIZE;
geo.start=0;
// while ((((geo.cylinders/2)*2)==geo.cylinders) && (geo.cylinders>1024)) {
while ((((hdsize/2)*2)==hdsize) && (hdsize>1024)) {
geo.sectors*=2;
// geo.cylinders/=2;
hdsize/=2;
// if (((geo.cylinders/2)*2)==geo.cylinders && (geo.cylinders>1024) ) {
if (((hdsize/2)*2)==hdsize && (hdsize>1024) ) {
geo.heads*=2;
// geo.cylinders/=2;
hdsize/=2;
}
}
geo.cylinders=hdsize;
if (hdsize != geo.cylinders) {
printf("Warning: %lli cylinders can not be stored in 16-bit value!\n", hdsize);
}
} else {
fprintf (stderr, "LSEEK: Error occured. Exit.\n");
exit(0);
}
}
printf("geometry: %d heads, %d secs, %d cyl, %ld start\n",
geo.heads,geo.sectors,geo.cylinders,geo.start);
i=geo.sectors*geo.heads*2;
if ((initsectors=malloc(i*SECTOR_SIZE))==NULL) {
fprintf (stderr,"Not enough memory for partition table.\n");
return -1;
}
rdb=RDSK(get_block(first));
memset(rdb,-1,SECTOR_SIZE);
rdb->rdb_ID = ntohl(IDNAME_RIGIDDISK);
rdb->rdb_SummedLongs = ntohl(64);
rdb->rdb_BlockBytes = ntohl(SECTOR_SIZE);
rdb->rdb_Flags = ntohl(0);
rdb->rdb_Cylinders = ntohl(geo.cylinders);
rdb->rdb_Sectors = ntohl(geo.sectors);
rdb->rdb_Heads = ntohl(geo.heads);
rdb->rdb_Interleave = ntohl(0);
rdb->rdb_Park = ntohl(geo.cylinders);
rdb->rdb_WritePreComp = ntohl(geo.cylinders);
rdb->rdb_ReducedWrite = ntohl(geo.cylinders);
rdb->rdb_StepRate = ntohl(0);
rdb->rdb_RDBBlocksLo = ntohl(first);
rdb->rdb_RDBBlocksHi = ntohl(geo.sectors*geo.heads*2-1);
rdb->rdb_LoCylinder = ntohl(2);
rdb->rdb_HiCylinder = ntohl(geo.cylinders-1);
rdb->rdb_CylBlocks = ntohl(geo.sectors*geo.heads);
rdb->rdb_AutoParkSeconds= ntohl(0);
rdb->rdb_HighRDSKBlock = ntohl(first);
firstblock=first;
lastblock=first;
minblock=first;
maxblock=geo.sectors*geo.heads*2-1;
rigiddisk_correct();
return 0;
}
int rigiddisk_reorg(int startblk)
{
/* Put all Blocks together and/or move RDB to another block */
char *curr, *curr2, *newblks;
int i,j,block,blk2;
ULONG *crk;
#ifdef DEBUG
printf ("Reorganizing RDB.\n");
#endif
i=countblk()+startblk-1;
/* Needed blocks = Starting Block + Allocated Blocks. Is this correct? */
if (i>maxblock) {
fprintf (stderr,"Not enough disk space to place RDB at Block %d. Have %d, need %d.\n",
startblk,maxblock,i);
return -1;
}
if ((newblks=malloc(maxblock*SECTOR_SIZE))==NULL) {
fprintf (stderr,"Not enough memory for reorganisation.\n");
return -1;
}
memset(newblks,-1,maxblock*SECTOR_SIZE);
j = (firstblock<startblk)?firstblock:startblk;
j = (j<2)?j:2;
if (j>0) printf ("Trying to save %d sector(s) \n",j);
for (i=0; i<j; i++) {
if (!valid_blocktype(htonl(((struct AmigaBlock *)(get_block(i)))->amiga_ID))) {
printf ("Copying raw block #%d (MBR?)\n",i);
memcpy (newblks+i*SECTOR_SIZE,initsectors+i*SECTOR_SIZE,SECTOR_SIZE);
}
}
i=startblk;
memcpy (newblks+i*SECTOR_SIZE,rdb,SECTOR_SIZE);
crk=&(RDSK(newblks+(startblk*SECTOR_SIZE))->rdb_BadBlockList);
i++;
curr=newblks+(i*SECTOR_SIZE);
block=htonl(rdb->rdb_BadBlockList);
while (block!=-1) {
memcpy (curr,initsectors+block*SECTOR_SIZE,SECTOR_SIZE);
(*crk)=ntohl(i);
crk=&(BBB(curr)->bbb_Next);
block=htonl(BBB(initsectors+block*SECTOR_SIZE)->bbb_Next);
i++;
curr=newblks+i*SECTOR_SIZE;
}
(*crk)=ntohl(-1);
crk=&(RDSK(newblks+(startblk*SECTOR_SIZE))->rdb_DriveInit);
block=htonl(rdb->rdb_DriveInit);
while (block!=-1) {
memcpy (curr,initsectors+(block*SECTOR_SIZE),SECTOR_SIZE);
(*crk)=ntohl(i);
crk=&(LSEG(curr)->lsb_Next);
block=htonl(LSEG(initsectors+block*SECTOR_SIZE)->lsb_Next);
i++;
curr=newblks+i*SECTOR_SIZE;
}
(*crk)=ntohl(-1);
crk=&(RDSK(newblks+startblk*SECTOR_SIZE)->rdb_FileSysHeaderList);
block=htonl(rdb->rdb_FileSysHeaderList);
while (block!=-1) {
memcpy (curr,initsectors+block*SECTOR_SIZE,SECTOR_SIZE);
(*crk)=ntohl(i);
crk=&(FSHB(curr)->fhb_SegListBlocks);
curr2=curr;
i++;
curr=newblks+i*SECTOR_SIZE;
blk2=htonl(FSHB(get_block(block))->fhb_SegListBlocks);
while (blk2!=-1) {
memcpy (curr,initsectors+blk2*SECTOR_SIZE,SECTOR_SIZE);
(*crk)=ntohl(i);
crk=&(LSEG(curr)->lsb_Next);
blk2=htonl(LSEG(initsectors+blk2*SECTOR_SIZE)->lsb_Next);
i++;
curr=newblks+i*SECTOR_SIZE;
}
crk=&(FSHB(curr2)->fhb_Next);
block=htonl(FSHB(initsectors+block*SECTOR_SIZE)->fhb_Next);
}
crk=&(RDSK(newblks+startblk*SECTOR_SIZE)->rdb_PartitionList);
block=htonl(rdb->rdb_PartitionList);
while (block!=-1) {
memcpy (curr,initsectors+block*SECTOR_SIZE,SECTOR_SIZE);
(*crk)=ntohl(i);
crk=&(PART(curr)->pb_Next);
block=htonl(PART(initsectors+block*SECTOR_SIZE)->pb_Next);
i++;
curr=newblks+i*SECTOR_SIZE;
}
lastblock=i;
firstblock=startblk;
minblock=startblk;
free (initsectors);
initsectors=newblks;
rdb=RDSK(initsectors+firstblock*SECTOR_SIZE);
rigiddisk_correct();
return 0;
}
int rigiddisk_save(void)
{
/* save RDB to disk */
int f;
rigiddisk_correct();
if ((f=open(disk_device,O_WRONLY))<0) {
fprintf (stderr,"Cannot open device %s\n",disk_device);
return -1;
}
if (lseek(f,0,SEEK_SET)<0) {
close(f); fprintf (stderr,"Seek error occured while writing partition table.\n");
return -1;
}
if (write(f,initsectors,maxblock*SECTOR_SIZE)!=maxblock*SECTOR_SIZE) {
close(f);
fprintf (stderr,"Error occured while writing partition table.\n");
return -1;
}
printf ("Rereading partition table.\n");
sync();
if(ioctl(f, BLKRRPART)) {
printf ("Could not reread partition table. You MUST reboot now to see the changes.\n");
return -1;
}
return 0;
}
char *DosType(ULONG dostype)
{
int i,j=0;
union {
ULONG dlong;
char dchar[4];
} dt;
dt.dlong = htonl(dostype);
if (!list_only) {
j = sprintf(type, "0x%04x = ",(unsigned int) dostype);
for (i = 0; i < 4; i++)
if (isprint(dt.dchar[i]))
type[j++] = dt.dchar[i];
else
j += sprintf(type + j, "\\%o", dt.dchar[i]);
j += sprintf(type + j, " ");
type[j]=0;
}
switch (ntohl(dt.dlong)) {
case 0x444f5300: j += sprintf(type + j, "Amiga OFS");break;
case 0x444f5301: j += sprintf(type + j, "Amiga FFS");break;
case 0x444f5302: j += sprintf(type + j, "Amiga OFS Int.");break;
case 0x444f5303: j += sprintf(type + j, "Amiga FFS Int.");break;
case 0x444f5304: j += sprintf(type + j, "Amiga OFS DirCache");break;
case 0x444f5305: j += sprintf(type + j, "Amiga FFS DirCache");break;
case 0x4C4E5800: j += sprintf(type + j, "Linux native");break;
case 0x53575000: j += sprintf(type + j, "Linux swap");break;
default: j += sprintf(type + j, "[unknown]");break;
}
return type;
}
int rigiddisk_show(void)
{
printf ("Disk %s: %ld heads, %ld sectors, %ld cylinders, RDB: %d\n", disk_device,
(long int) htonl(rdb->rdb_Heads), (long int) htonl(rdb->rdb_Sectors), (long int) htonl(rdb->rdb_Cylinders), firstblock);
printf ("Logical Cylinders from %ld to %ld, %ld bytes/Cylinder \n\n",
(long int) htonl(rdb->rdb_LoCylinder), (long int) htonl(rdb->rdb_HiCylinder), (long int) SECTOR_SIZE*htonl(rdb->rdb_Sectors)*htonl(rdb->rdb_Heads));
return 0;
}
int partition_show(void)
{
int i,block;
struct DosEnvec *de;
struct PartitionBlock *curr;
block=htonl(rdb->rdb_PartitionList);
printf (" Device Boot Mount Begin End Size Pri BBlks System\n");
for (i=0; i<parts; i++) {
curr=PART(get_block(block));
block=htonl(curr->pb_Next);
de=(struct DosEnvec *)curr->pb_Environment;
printf ("%s%-2d ",disk_device,(i+1));
printf ("%s ", htonl(curr->pb_Flags)&PBFF_BOOTABLE?"*":" ");
printf ("%s ", htonl(curr->pb_Flags)&PBFF_NOMOUNT?" ":"*");
printf ("%6ld %6ld ", (long int) htonl(de->de_LowCyl), (long int) htonl(de->de_HighCyl));
printf ("%6ld ", (long int) (htonl(de->de_HighCyl)-htonl(de->de_LowCyl)+1)*
htonl(de->de_BlocksPerTrack)*htonl(de->de_Surfaces)/2);
printf ("%3ld ",(long int) htonl(de->de_BootPri));
printf ("%3ld ",(long int) htonl(de->de_BootBlocks));
printf ("%s\n",DosType(htonl(de->de_DosType)));
}
return 0;
}
int quitall(void)
{
if (!list_only) {
free (initsectors);
} else
free (listsector);
return 0;
}
|