game.c 20.8 KB
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

/*
 * Main program for R4300 for a disk write program. Since this program is
 * using host io protocol and host io protocol uses Int 1 and cart int, we
 * have to do a certain tricky technique to share these interrupts between
 * host io and 64 disk drive. The method I used here is "changing" the 
 * interrupt service routine of Int 1 and the message queue that the message
 * from cartridge will be sent. See the source code of osLeoSwitch() for
 * more information.
 */
#include "hostio.h"

/*
 * Stacks for the threads as well as message queues for synchronization
 * This stack is ridiculously large, and could also be reclaimed once
 * the main thread is started.
 */
u64	bootStack[STACKSIZE/sizeof(u64)];

static void	idle(void *);
static void	mainproc(void *);

static OSThread	idleThread;
static u64	idleThreadStack[STACKSIZE/sizeof(u64)];

static OSThread	mainThread;
static u64	mainThreadStack[5 * STACKSIZE/sizeof(u64)];

/*
 * Message queues
 */
#define NUM_PI_MSGS     8
static OSMesg PiMessages[NUM_PI_MSGS];
static OSMesgQueue PiMessageQ;

static OSMesgQueue	diskQ;		/* post queue */
static OSMesg		diskQBuf;

#define NUM_CMD_MSGS	8
static OSMesg           cmdQBuf[8];

/*
 * Tables to show where the system, defect and error ecc blocks are.
 */
s32	sysLBAs[][4] 	= {
  { -24, -23, -16, -15 },  /* for JAPAN */
  { -22, -21, -14, -13 },  /* for DEV   */
  { -24, -23, -16, -15 },  /* for USA   */
};
s32	defLBAs[] 	= { -20, -19 };
s32	idLBAs[] 	= { -10, -9 };

#define	ECC_LBA		-12

static char	dummybuf[232 * SECPERBLOCK + 15];
static char	dummybuf2[240 * SECPERBLOCK + 15];
sysInfo	*systemData;
u32	driveType;

extern  s32 LeoSysReadWrite(LEOCmd *, s32, s32, void *,
			    u32, OSMesgQueue *);

/*
 * Prototypes
 */
static	s32   	error_handle(s32);
static 	void 	formatDisk(ctrlblk *);
static 	void 	setID(ctrlblk *);
static	void 	checkECC(void);
static	s32 	readSysArea(sysInfo *sbuf);
static	s32 	readIdArea(idInfo *ibuf);
static	void 	getDiskInfo(ctrlblk *sdata);

/*
 * Definition for leoSwitch
 */
#define	DISK	0
#define	INDY	1

/*
 * Definition for error handling routine
 */
#define	RETRY	0
#define	FATAL	1

/*
 * These variables are defined in each file to align 16bytes.
 * These are for communication to Indy.
 */
extern	u64	rdramBuf[];
extern	ctrlblk	data;
extern	s32	transbyte;

extern u32 LEO_country_code;

void
boot(void)
{
    
  osInitialize();
  
  osCreateThread(&idleThread, 1, idle, (void *)0,
		 idleThreadStack+STACKSIZE/sizeof(u64), 10);
  osStartThread(&idleThread);
  
  /* never reached */
}

static void
idle(void *arg)
{
  /*
   * Create main thread
   */
  osCreateThread(&mainThread, 3, mainproc, arg,
		 mainThreadStack+STACKSIZE/sizeof(u64), 10);
  osStartThread(&mainThread);
  
  /*
   * Become the idle thread
   */
  osSetThreadPri(0, 0);
  
  for (;;);
}

/*
 * This is the main routine of the app.
 */
static void
mainproc(void *arg)
{
  LEOError	error;
  LEOCmd	cmdBlock;
  LEODiskID	dummyDiskID;
  s32		curLBA;
  u32		transLBA;
  u32		needLBAs;
  u32		bufferLBAs;
  u32		remain;
  u32		result;
  u32		i;
  
  systemData = (sysInfo *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf);

  osCreateMesgQueue(&diskQ, &diskQBuf, 1);

  osCreatePiManager((OSPri)OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
		    NUM_PI_MSGS);

  error = LeoCreateLeoManager((OSPri)OS_PRIORITY_LEOMGR-1,
			      (OSPri)OS_PRIORITY_LEOMGR,
			      cmdQBuf, NUM_CMD_MSGS);

  if (error == LEO_ERROR_DEVICE_COMMUNICATION_FAILURE)
  {
    osSyncPrintf("leowrite: Drive doesn't exist\n");
    for(;;);
  }
  
  /*
   * Clear "reset" flag.
   */
  LeoResetClear();

  /*
   * モータはあまり止まらないように設定
   */
  LeoModeSelectAsync(&cmdBlock, 10, 10, &diskQ);
  osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

  while(1)			/* main loop */
  {
    /*
     * Send 'Ready' to Indy
     */
    osWriteHost(rdramBuf, 4);

    /*
     * First, we get the type, filesize and offset.
     */
    osReadHost(&data, sizeof(ctrlblk));

    if (data.cmdFlags & END_COMMAND)
    {
      /*
       * End of leowrite. Stop the motor of the drive
       */
      do
      {
	LeoSpdlMotor(&cmdBlock, SLEEP, &diskQ);
	osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);
    
	if ((GET_ERROR(cmdBlock) == 0) || 
	    (GET_ERROR(cmdBlock) == LEO_ERROR_MEDIUM_NOT_PRESENT))
	  break;
	if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
    
      } while(1);
    
      /*
       * Send back the end command
       */
      osWriteHost(&data, sizeof(ctrlblk));
  
      /* Wait host to kill me */
      for(;;);

    }
  
    if (data.cmdFlags & SEND_ME_INFO)
    {
      getDiskInfo(&data);
    
      if ( !(data.cmdFlags & SYS_UNFORMATTED) ) /* if sys is unformatted, might hang */
      {
	/*
	 * Stop the motor of the drive
	 */
	do
	{
	  LeoSpdlMotor(&cmdBlock, SLEEP, &diskQ);
	  osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);
    
	  if ((GET_ERROR(cmdBlock) == 0) || 
	      (GET_ERROR(cmdBlock) == LEO_ERROR_MEDIUM_NOT_PRESENT))
	    break;
	  if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	  {
	    osSyncPrintf("Fatal error\n");
	    for(;;);
	  }
    
	} while(1);
      }
    
      osWriteHost(&data, sizeof(ctrlblk));

      continue;
      
    } /* if (SEND_ME_INFO) */
  
    /*
     * If the system area has never been written, format the disk.
     * Otherwise, change the area according as the specified parameters.
     */
    formatDisk(&data);
  
    /*
     * Set ID number
     */
    setID(&data);
  
    /*
     * Do dummy "readid" to read system info
     */
    do{
      LeoReadDiskID(&cmdBlock, &dummyDiskID, &diskQ);
      osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

      if(GET_ERROR(cmdBlock) == 0)
	break;
      if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);

    curLBA = data.offset;
    /*
     * If the filesize is larger than the size of the remained area,
     * we quit the operation here.
     */
    do{
      result = LeoLBAToByte(curLBA, NUM_LBAS - curLBA, (s32 *)&remain);

      if(result == 0)
	break;
      if (error_handle(result) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);
  
    if (remain < data.filesize)
    {
      osSyncPrintf("file is too large: remaining disk space %d, filesize %d\n",
		   remain, data.filesize);
      transbyte = -1;
      osWriteHost(&transbyte, sizeof(transbyte));
    
      /* Wait host to kill me */
      for(;;);
    }
  
    /*
     * Calculate how many lbas are needed to write the file.
     */
    do{
      result = LeoByteToLBA(curLBA, data.filesize, (s32 *)&needLBAs);
      if (result == 0)
	break;
      if (error_handle(result) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);

    osSyncPrintf("Use from LBA%d to %u", curLBA,
		 curLBA + needLBAs - 1);
  
    osSyncPrintf("(%d LBA(s))\n", needLBAs);

    /*
     * Main loop
     */
    while(data.filesize > 0)
    {
      /*
       * Calculate how many lbas can be stored in the RDRAM buffer.
       */
      if (BUFSIZE > remain)
	bufferLBAs = NUM_LBAS - curLBA;
      else
      {
	do{
	  result = LeoByteToLBA(curLBA, BUFSIZE, (s32 *)&bufferLBAs);

	  if(result == 0)
	    break;
	  if (error_handle(result) == FATAL)
	  {
	    osSyncPrintf("Fatal error\n");
	    for(;;);
	  }
	} while(1);

	/*
	 * Since BUFSIZE is smaller than the size of bufferLBAs,
	 * decrement bufferLBAs.
	 * XXX we might miss one loop (when BUFSIZE = bufferLBAs)
	 */
	bufferLBAs --;
      }

      /*
       * Calculate how many lbas and bytes should be transferred this
       * time.
       */
      transLBA = Min(needLBAs, bufferLBAs);
      do{
	result = LeoLBAToByte(curLBA, transLBA, (s32 *)&transbyte);

	if(result == 0)
	  break;
	if (error_handle(result) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
      } while(1);
    
      /* XXX need to fix a bug. What if filesize = 10000, remain = 19720 and curLBA = 0? */

      /*
       * Ask host to transfer <transbyte> bytes
       */
      osWriteHost(&transbyte, sizeof(transbyte));
    
      osReadHost(rdramBuf, transbyte);
    
      /*
       * Write <transLBA> blocks to disk
       */
      do{
	LeoSysReadWrite(&cmdBlock, OS_WRITE,
			(u32)curLBA, (void *)rdramBuf, (u32)transLBA, &diskQ);
	osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

	if(GET_ERROR(cmdBlock) == 0)
	  break;
	if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
      } while(1);

      curLBA += transLBA;
      needLBAs -= transLBA;
      data.filesize -= transbyte;
      remain -= transbyte;
    
    } /* while */

    /*
     * Send end mark to host
     */
    transbyte = -1;
    osWriteHost(&transbyte, sizeof(transbyte));

  } /* while */
  
} /* mainproc() */

/*
 * Error handling routine. In this version, I don't check the ID blocks.
 * XXX need to fix
 */
s32 error_handle(s32 result)
{
  LEOCmd	cmdBlock;
  LEODiskID	dummyDiskID;
  LEOStatus	test;
  s32		res;
  
  /*
   * If the error is not one of these two, consider it as fatal.
   */
  if (!( (result == LEO_ERROR_MEDIUM_NOT_PRESENT) ||
      (result == LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED ) ))
    return FATAL;
  
  /*
   * Since this version doesn't check the ID, I don't care if the disk
   * might have been changed.
   */
  if (result == LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED)
    return RETRY;
  
  osSyncPrintf("Insert a disk\n");

  /*
   *  Wait for the users to insert the disk.
   *  (myLeoReadDiskID command doesn't check the system area).
   */
  do
  {
    myLeoReadDiskID(&cmdBlock, &dummyDiskID, &diskQ);
    osRecvMesg(&diskQ, (OSMesg *)&res, OS_MESG_BLOCK);
    
    if ( res != LEO_ERROR_MEDIUM_NOT_PRESENT )
      break;

  } while(1);
  
  return RETRY;

} /* error_handle() */


void checkECC(void)
{
  u8	*ebuf;
  LEOCmd	cmdBlock;

  ebuf = (u8 *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf2);

  /*
   * Read the error ECC block to check the drive type. If the read is 
   * successful, the drive should be production type. If not, it should
   * be development type.
   */
  do{
    LeoSysReadWrite(&cmdBlock, OS_READ,
		    ECC_LBA, (void *)ebuf, 1, &diskQ);
    osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

    if( GET_ERROR(cmdBlock) == 0)
    {
      driveType = DEV;
      LEO_country_code = 0;
      SAVE_COUNTRY(0);		/* paranoia */
      return;
    }

    /*
     * エラー23の場合、日本向けとアメリカ向けの2通りの可能性があるが、
     * とりあえずシステム領域を読むまでは日本向けとしておく。
     */
    if ( GET_ERROR(cmdBlock) == LEO_ERROR_UNRECOVERED_READ_ERROR )
    {
      driveType = PROD;
      LEO_country_code = JAPAN;
      SAVE_COUNTRY(JAPAN);	/* paranoia */
      return;
    }
    
    if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
    {
      osSyncPrintf("Fatal error\n");
      for(;;);
    }
  } while(1);

} /* checkECC() */

#define	FORMATTED	0
#define	UNFORMATTED	1

s32 readSysArea(sysInfo *sbuf)
{
  LEOCmd	cmdBlock;
  s32		rval;

  do{
    LeoSysReadWrite(&cmdBlock, OS_READ,
		    sysLBAs[driveType][0], (void *)sbuf, 1, 
		    &diskQ);
    osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

    if(GET_ERROR(cmdBlock) == 0)
    {
      rval = FORMATTED;
      break;
    }
    if(GET_ERROR(cmdBlock) == LEO_ERROR_UNRECOVERED_READ_ERROR)
    {
      return UNFORMATTED;
    }
    if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
    {
      osSyncPrintf("Fatal error\n");
      for(;;);
    }
  } while(1);

  if (sbuf->prod[0].country == USA)
  {
    driveType = PRODUSA;
    osSyncPrintf("This is a prod(USA) type\n");
    LEO_country_code = USA;
    SAVE_COUNTRY(USA);	/* paranoia */
  }
  else if (sbuf->prod[0].country == JAPAN)
    osSyncPrintf("This is a prod(JAPAN) type\n");
  else if (sbuf->dev[0].country == 0)
    osSyncPrintf("This is a dev type\n");
  else
    osSyncPrintf(".....Can't understand country code\n");
  
  return rval;

} /* readSysArea() */


s32 readIdArea(idInfo *ibuf)
{
  LEOCmd	cmdBlock;

  do{
    LeoSysReadWrite(&cmdBlock, OS_READ,
		    idLBAs[0], (void *)ibuf, 1, 
		    &diskQ);
    osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

    if(GET_ERROR(cmdBlock) == 0)
      return FORMATTED;
    else if(GET_ERROR(cmdBlock) == LEO_ERROR_UNRECOVERED_READ_ERROR)
    {
      bzero((void *)ibuf, 232 * SECPERBLOCK);
      return UNFORMATTED;
    }
      
    if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
    {
      osSyncPrintf("Fatal error\n");
      for(;;);
    }
  } while(1);

} /* readIdArea() */


void getDiskInfo(ctrlblk *sdata)
{
  sysInfo	*sbuf;
  idInfo	*ibuf;
  
  sbuf = (sysInfo *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf2);

  checkECC();

  if(readSysArea(sbuf) == UNFORMATTED)
  {
    sdata->cmdFlags |= SYS_UNFORMATTED;
    return;
  }

  if (driveType == DEV)
  {
    sdata->offset 	= ((sysInfoDev *)sbuf)[0].IPLSize;
    sdata->type 	= ((sysInfoDev *)sbuf)[0].type & 0x0f;
    sdata->address 	= ((sysInfoDev *)sbuf)[0].loadAddr;
  }
  else
  {
    sdata->cmdFlags	|= DRIVE_PROD;
    sdata->offset 	= ((sysInfoProd *)sbuf)[0].IPLSize;
    sdata->type 	= ((sysInfoProd *)sbuf)[0].type & 0x0f;
    sdata->address 	= ((sysInfoProd *)sbuf)[0].loadAddr;
    sdata->romEndLBA    = ((sysInfoProd *)sbuf)[0].romEndLBA;
    sdata->ramStartLBA  = ((sysInfoProd *)sbuf)[0].ramStartLBA;
    sdata->ramEndLBA    = ((sysInfoProd *)sbuf)[0].ramEndLBA;
  }

  ibuf = (idInfo *)sbuf;
  
  if(readIdArea(ibuf) == UNFORMATTED)
  {
    sdata->cmdFlags |= ID_UNFORMATTED;
    return;
  }

/*  bcopy((void *)&ibuf->diskID.serialNumber,
	(void *)&sdata->diskID.serialNumber, sizeof(LEOSerialNum));
*/
  bcopy((void *)&ibuf->diskID,
	(void *)&sdata->diskID, sizeof(LEODiskID));
  
  return;
  
} /* getDiskInfo */


void formatDisk(ctrlblk *sdata)
{
  LEOCmd	cmdBlock;
  u32		i;
  sysInfo	*defData;
  u8		overwrite;

  defData = (sysInfo *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf2);

  /*
   * Decide which type this drive is, prod or dev?
   */
  checkECC();
  
  /*
   * Decide whether to format(overwrite) or not. If -f option is 
   * specified in the command line, format it. If not and the system
   * information block is not readable, format it. Otherwise, use
   * the previously-written system information.
   */
  if ((sdata->cmdFlags & DO_FORMAT) || (readSysArea(systemData) == UNFORMATTED))
  {
    osSyncPrintf("Formatting the disk as ");
    
    for (i = 0; i < 240 * 85/sizeof(u32); i++)
      ((u32 *)defData)[i] = 0xffffffff;

    /*
     * Write bad ecc
     */
    if ((sdata->cmdFlags & DO_FORMAT_MASK) == DO_FORMAT_AS_PROD)
    {
      osSyncPrintf("prod(JAPAN) type\n");
      osSyncPrintf(" (use -F1 option to format as prod(USA) type)\n");
      osSyncPrintf(" (use -f option to format as dev type)\n");
      driveType = PROD;
      LEO_country_code = JAPAN;
      SAVE_COUNTRY(JAPAN);	/* paranoia */

      /*
       * Write to the disk using "long write", which we can
       * write C1 area directly.
       */
      do{
	LeoReadWriteLong(&cmdBlock, OS_WRITE, -12,
			 (void *)defData, 1, 0, &diskQ);
	osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

	if( GET_ERROR(cmdBlock) == 0)
	  break;
	if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
      } while(1);

    }
    else if ((sdata->cmdFlags & DO_FORMAT_MASK) == DO_FORMAT_AS_PRODUSA)
    {
      osSyncPrintf("prod(USA) type\n");
      osSyncPrintf(" (use -F option to format as prod(JAPAN) type)\n");
      osSyncPrintf(" (use -f option to format as dev type)\n");
      driveType = PRODUSA;
      LEO_country_code = USA;
      SAVE_COUNTRY(USA);	/* paranoia */

      /*
       * Write to the disk using "long write", which we can
       * write C1 area directly.
       */
      do{
	LeoReadWriteLong(&cmdBlock, OS_WRITE, -12,
			 (void *)defData, 1, 0, &diskQ);
	osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

	if( GET_ERROR(cmdBlock) == 0)
	  break;
	if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
      } while(1);

    }
    else
    {
      osSyncPrintf("dev type\n");
      osSyncPrintf(" (use -F option to format as prod(JAPAN) type)\n");
      osSyncPrintf(" (use -F1 option to format as prod(USA) type)\n");
      driveType = DEV;
      LEO_country_code = 0;
      SAVE_COUNTRY(0);		/* paranoia */

      /*
       * Write to the disk using the normal read command,
       * which makes this block readable.
       */
      do{
	LeoSysReadWrite(&cmdBlock, OS_WRITE, -12,
			(void *)defData, 1, &diskQ);
	osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);
	
	if( GET_ERROR(cmdBlock) == 0)
	  break;
	if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
	{
	  osSyncPrintf("Fatal error\n");
	  for(;;);
	}
      } while(1);

    }

    /*
     * Read the defect information block. It's readable now since the country
     * code has been set correctly.
     */
    do{
      LeoSysReadWrite(&cmdBlock, OS_READ,
		      defLBAs[0], (void *)defData, 1, &diskQ);
      osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

      if( GET_ERROR(cmdBlock) == 0)
	break;
      if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
      {
	osSyncPrintf("Fatal error: can't read defect info\n");
	osSyncPrintf("....Check the type of the drive and diskette.\n");
	for(;;);
      }
    } while(1);
    
    /*
     * Copy the defect information block to the sys. info. buffer.
     */
    bcopy(defData, systemData, 232 * SECPERBLOCK);

    /*
     * Copy the format byte to the disk type byte.
     */
    if ((driveType == PROD) || (driveType == PRODUSA))
      for (i = 0; i < SECPERBLOCK; i++)
	systemData->prod[i].type	= systemData->prod[i].formatType;
    else
      for (i = 0; i < SECPERBLOCK; i++)
	systemData->dev[i].type		= systemData->dev[i].formatType;
  }  
  
  /*
   * Put the specified values to the buffer
   */
  if (driveType == PROD)
  {
    for(i = 0; i < SECPERBLOCK; i++)
    {
      systemData->prod[i].country	= JAPAN;
      if (sdata->type != TYPE_DONT_CHANGE)
	systemData->prod[i].type    	= sdata->type + (systemData->prod[i].type & 0xf0);
      if (sdata->offset == 0)
	systemData->prod[i].IPLSize 	= (u32)((data.filesize + 19719) / 19720);
      if (sdata->address != NULL)
	systemData->prod[i].loadAddr	= sdata->address;
      if (sdata->cmdFlags & WRITE_DUP_INFO)
      {
	systemData->prod[i].romEndLBA	= sdata->romEndLBA;
	systemData->prod[i].ramStartLBA	= sdata->ramStartLBA;
	systemData->prod[i].ramEndLBA	= sdata->ramEndLBA;
      }
	
    }
    osSyncPrintf("type = %d, addr = 0x%x\n",
		 systemData->prod[0].type & 0x0f, 
		 systemData->prod[0].loadAddr);
  }
  else if (driveType == PRODUSA)
  {
    for(i = 0; i < SECPERBLOCK; i++)
    {
      systemData->prod[i].country	= USA;
      if (sdata->type != TYPE_DONT_CHANGE)
	systemData->prod[i].type    	= sdata->type + (systemData->prod[i].type & 0xf0);
      if (sdata->offset == 0)
	systemData->prod[i].IPLSize 	= (u32)((data.filesize + 19719) / 19720);
      if (sdata->address != NULL)
	systemData->prod[i].loadAddr	= sdata->address;
      if (sdata->cmdFlags & WRITE_DUP_INFO)
      {
	systemData->prod[i].romEndLBA	= sdata->romEndLBA;
	systemData->prod[i].ramStartLBA	= sdata->ramStartLBA;
	systemData->prod[i].ramEndLBA	= sdata->ramEndLBA;
      }
	
    }
    osSyncPrintf("type = %d, addr = 0x%x\n",
		 systemData->prod[0].type & 0x0f, 
		 systemData->prod[0].loadAddr);
  }
  else
  {
    for(i = 0; i < SECPERBLOCK; i++)
    {
      systemData->dev[i].country	= 0;
      if (sdata->type != TYPE_DONT_CHANGE)
	systemData->dev[i].type    	= sdata->type + (systemData->dev[i].type & 0xf0);
      if (sdata->offset == 0)
	systemData->dev[i].IPLSize 	= (u32)((data.filesize + 19719) / 19720);
      if (sdata->address != NULL)
	systemData->dev[i].loadAddr	= sdata->address;
    }
    osSyncPrintf("type = %d, addr = 0x%x\n",
		 systemData->dev[0].type & 0x0f, 
		 systemData->dev[0].loadAddr);
  }
  
  /*
   * Write it to the system information block
   */
  for (i = 0; i < 4; i++)
    do{
      LeoSysReadWrite(&cmdBlock, OS_WRITE,
		      sysLBAs[driveType][i], (void *)systemData, 1, 
		      &diskQ);
      osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

      if( GET_ERROR(cmdBlock) == 0)
	break;
      if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);

  return;

} /* formatDisk() */


void setID(ctrlblk *sdata)
{
  LEOCmd	cmdBlock;
  idInfo	*idData;
  u32		i;
  
  idData = (idInfo *)OS_DCACHE_ROUNDUP_ADDR((void *)dummybuf2);
  
  /*
   * Try reading the ID area. If succeeded, simply return.
   */
  readIdArea(idData);
  
  if (sdata->cmdFlags & SPECD_ID_FILE)
    for(i = 0; i < SECPERBLOCK; i++)
    {
      bcopy((void *)&sdata->diskID,
	    (void *)&idData[i].diskID, sizeof(LEODiskID));
      bzero((void *)&idData[i].diskmanID, 200);
    }
  else
    for(i = 0; i < SECPERBLOCK; i++)
    {
      bcopy((void *)&sdata->diskID.serialNumber,
	    (void *)&idData[i].diskID.serialNumber, sizeof(LEOSerialNum));
      bzero((void *)&idData[i].diskmanID, 200);
    }
  
  for (i = 0; i < 2; i++)
    do{
      LeoSysReadWrite(&cmdBlock, OS_WRITE,
		      idLBAs[i], (void *)idData, 1, 
		      &diskQ);
      osRecvMesg(&diskQ, NULL, OS_MESG_BLOCK);

      if(GET_ERROR(cmdBlock) == 0)
	break;
      if (error_handle(GET_ERROR(cmdBlock)) == FATAL)
      {
	osSyncPrintf("Fatal error\n");
	for(;;);
      }
    } while(1);
  
  return;

} /* setID() */