home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / ranish / SOURCES.ZIP / S_FAT32.C < prev    next >
C/C++ Source or Header  |  1998-09-12  |  21KB  |  648 lines

  1. #include "part.h"
  2.  
  3.  
  4. struct boot_fat32
  5.     {
  6.      /* Sector 1 */
  7.      
  8.      unsigned char  jmp[3];    /* Must be 0xEB, 0x58, 0x90 = jmp 5A    */
  9.      unsigned char  sys_id[8];    /* Probably:   "MSWIN4.1"        */
  10.  
  11.      unsigned short sect_size;    /* Sector size in bytes (512)        */
  12.      unsigned char  clust_size;    /* Sectors per cluster (1,2,4,...,128)    */
  13.      unsigned short res_sects;    /* Reserved sectors at the beginning (33)*/
  14.      unsigned char  fat_copies;    /* Number of FAT copies (2)        */
  15.  
  16.      unsigned char  resrvd1[4];    /* Reserved                */
  17.      unsigned char  media_desc;    /* Media descriptor byte (F8h)        */
  18.      unsigned short sfat_size;    /* Sectors per FAT            */
  19.  
  20.      unsigned short track_size;    /* Sectors per track            */
  21.      unsigned short num_sides;    /* Sides                */
  22.  
  23.      unsigned long  hid_sects;    /* Special hidden sectors        */
  24.      unsigned long  num_sects;    /* Big total number of sectors      */
  25.      unsigned long  fat_size;    /* Sectors per FAT (big)        */
  26.      unsigned char  fat_attr;    /* FAT attributes (I guess)        */
  27.  
  28.      unsigned char  fs_ver_maj;    /* File System Version (major)        */
  29.      unsigned short fs_ver_min;    /* File System Version (minor)        */
  30.  
  31.      unsigned long  root_clust;    /* First cluster in root        */
  32.  
  33.      unsigned short fs_sect_num; /* FS Sector number (1) ???        */
  34.      unsigned short bs_bak_sect; /* Boot sector backup (6)        */
  35.  
  36.      unsigned char  resrvd2[12];    /* Reserved                */
  37.      unsigned char  drive_num;    /* Physical drive number (80h)        */
  38.      unsigned char  resrvd3[1];    /* Reserved                */
  39.  
  40.      unsigned char  ext_signat;    /* Extended Boot Record signature (29h)    */
  41.      unsigned long  serial_num;    /* Volume serial number            */
  42.      unsigned char  label[11];    /* Volume label                */
  43.      unsigned char  fs_id[8];    /* File system id ("FAT32   ")        */
  44.      unsigned char  xcode[418];    /* Boot loader code (first part)    */
  45.      unsigned long  magic_num;    /* Magic number (Must be 0xAA550000)     */
  46.      
  47.      /* Sector 2 */
  48.      
  49.      unsigned long  ext_sign2;     /* Ext Boot Record Sign (0x41615252)    */
  50.      unsigned char  resrvd4[480];/* Reserved                */
  51.      unsigned long  ext_sign3;     /* FS Info Signature    (0x61417272)    */
  52.      unsigned long  free_clust;     /* Number of free clusters        */
  53.      unsigned long  next_free;     /* Next free cluster            */
  54.      unsigned char  resrvd5[12]; /* Reserved                */
  55.      unsigned long  magic_num2;     /* Ext Boot Record Sign (0xAA550000)    */
  56.  
  57.      /* Sector 3 */
  58.  
  59.      unsigned char  resrvd6[508];/* Reserved                */
  60.      unsigned long  magic_num3;     /* Ext Boot Record Sign (0xAA550000)    */
  61.     };
  62.  
  63. #define BBT_SIZE 12000
  64.  
  65. #define F_NORM  0
  66. #define F_QUICK 1
  67. #define F_DESTR 2
  68.  
  69. #define MBR_MAGIC_NUM32 0xAA550000L
  70.  
  71.  
  72. int format_fat32(struct part_long *p, char **argv)
  73. {
  74.  char *data_pool;
  75.  struct boot_fat32 *b;
  76.  unsigned long *fat;
  77.  int i, j, k, wr_sect, ret_code, fat_size, next_bad;
  78.  unsigned long l, num_clust, num_sect, base_sect, base_clust, *bbt;
  79.  
  80.  unsigned int num_bad=0;
  81.  unsigned int clust_size=8;
  82.  unsigned int form_type=F_NORM;
  83.  
  84.  if( (data_pool=malloc(SECT_SIZE*3+BBT_SIZE*sizeof(long)))==0 )
  85.    {
  86.     show_error(ERROR_MALLOC);
  87.     return FAILED;
  88.    }
  89.  
  90.  b   = (struct boot_fat32*)(data_pool);
  91.  fat = (unsigned long int*)(data_pool);
  92.  bbt = (unsigned long int*)(data_pool+SECT_SIZE*3);
  93.  
  94.  memset(b,0,SECT_SIZE*3);
  95.  
  96.  memmove(b->jmp,"\xEB\x58\x90",3);
  97.  memmove(b->sys_id,"MSWIN4.1",8);
  98.  b->sect_size=SECT_SIZE;
  99.  b->res_sects=33;
  100.  b->fat_copies=2;
  101.  b->fs_sect_num=1;
  102.  b->bs_bak_sect=6;
  103.  b->media_desc=0xF8;
  104.  b->ext_signat=0x29;
  105.  memmove(b->label,"NO NAME    ",11);
  106.  memmove(b->fs_id,"FAT32   ",8);
  107.  memmove(b->xcode, EMP_IPL, EMP_SIZE );
  108.  strncpy(b->xcode + EMP_SIZE, MESG_NON_SYSTEM, sizeof(b->xcode)-EMP_SIZE);
  109.  
  110.  b->ext_sign2 = 0x41615252L;
  111.  b->ext_sign3 = 0x61417272L;
  112.  
  113.  b->magic_num = 0xAA550000L;
  114.  b->magic_num2= 0xAA550000L;
  115.  b->magic_num3= 0xAA550000L;
  116.  
  117.  num_sect=p->num_sect;
  118.  
  119.  while(*argv!=0)
  120.     {
  121.      if( strcmpi(*argv,"/destructive")==0 ) form_type=F_DESTR;
  122.      else if(  strcmpi(*argv,"/quick")==0 ) form_type=F_QUICK;
  123.      else if( strncmpi(*argv,"/c:", 3)==0 )
  124.        {
  125.         k=atoi((*argv)+3);
  126.         for( i=k/2, j=0 ; i!=0 && j<7 ; j++, i/=2 );
  127.         clust_size=1<<j;
  128.         if( clust_size!=k )
  129.           {
  130.            progress("^Invalid cluster size.");
  131.            goto failed;
  132.           }
  133.        }
  134.      else if( strncmpi(*argv,"/x:", 3)==0 )
  135.        {
  136.         if( strcmpi(*argv,"/x:disk")==0 )
  137.           l=dinfo.total_sects;
  138.         else
  139.           l=atol((*argv)+3);
  140.         if( l>num_sect ) num_sect=l;
  141.        }
  142.      else if( strncmpi(*argv,"/l:", 3)==0 )
  143.        {
  144.         strncpy(tmp,(*argv)+3,11);
  145.         tmp[11]=0;
  146.         for( i=0 ; tmp[i]!=0 && i<11 ; i++ ) b->label[i]=tmp[i];
  147.         for( ; i<11 ; i++ ) b->label[i]=' ';
  148.        }
  149.      else
  150.        {
  151.         progress("^Unknown option:");
  152.         progress(*argv);
  153.         goto failed;
  154.        }
  155.      argv++;
  156.     }
  157.  
  158.  while(1)
  159.     {
  160.      fat_size =  (num_sect)/(clust_size*128) + 1;
  161.      num_clust = (num_sect-33-2*fat_size)/(clust_size);
  162.      
  163.      if( num_clust >= 65536L ) break;
  164.      
  165.      clust_size /= 2;
  166.      
  167.      if( clust_size==0 )
  168.        {
  169.         progress("^Oops! Cluster size is 0...");
  170.         progress("You have to use FAT-16 file system for such a small partition.");
  171.         goto failed;
  172.        }
  173.     }
  174.      
  175.  
  176.  if( 33+2*fat_size+2*clust_size > p->num_sect )
  177.    {
  178.     progress("^Partition is too small.");
  179.     goto failed;
  180.    }
  181.  
  182.  
  183.  b->fat_size=fat_size;
  184.  b->clust_size=clust_size;
  185.  
  186.  b->root_clust=2;
  187.  b->next_free=3;
  188.  b->free_clust=num_clust-1;
  189.  
  190.  b->drive_num=dinfo.disk;
  191.  b->track_size=dinfo.num_sects;
  192.  b->num_sides=dinfo.num_heads;
  193.  
  194.  b->hid_sects = p->rel_sect;
  195.  b->num_sects = p->num_sect;
  196.  
  197.  b->serial_num=((p->rel_sect<<16)+(p->num_sect*((long)b%451)) )+
  198.                ((dinfo.total_sects%12345L)^(dinfo.total_sects*67891L))+
  199.                ((dinfo.disk*123L)^(dinfo.num_heads%7))+clock();
  200.  
  201.  flush_caches();
  202.  
  203.       if( form_type==F_QUICK ) ret_code=0;
  204.  else if( form_type==F_DESTR )
  205.    ret_code=generic_format(p,BBT_SIZE,bbt);
  206.  else
  207.    ret_code=generic_verify(p,BBT_SIZE,bbt);
  208.  
  209.  if( ret_code<0 )      /* format failed or canceled */
  210.    {
  211.     free(data_pool);
  212.     return ret_code;
  213.    }
  214.  
  215.  num_bad=ret_code;
  216.  
  217.  disk_lock(dinfo.disk);
  218.  
  219.  progress("^Initializing file system ...");
  220.  
  221.  if( num_bad!=0 && bbt[0] < 33+2*fat_size+2*clust_size )
  222.    {
  223.     progress("Beginning of the partition is unusable. Try to move it forward.");
  224.     goto failed;
  225.    }
  226.  
  227.  progress("~Writing boot sector ...");
  228.  
  229.  if( disk_write_rel(p,0,b,3)==-1 )    /*  Writing boot sector  */
  230.    {
  231.     progress("Error writing boot sector.");
  232.     goto failed;
  233.    }
  234.  
  235.  if( disk_write_rel(p,6,b,3)==-1 )    /*  Writing boot sector (backup) */
  236.    {
  237.     progress("Error writing boot sector.");
  238.     goto failed;
  239.    }
  240.  
  241.  progress("~Writing FAT tables ...");
  242.  
  243.  wr_sect = 33;
  244.  
  245.  for( k=0 ; k<2 ; k++ )         /* Writing two copies of FAT16 */
  246.     {
  247.      next_bad = 0;
  248.      base_sect = 33 + 2*fat_size + clust_size /*root*/;
  249.  
  250.      for( i=0 ; i<fat_size ; i++ )
  251.         {
  252.          memset(fat,0,512);
  253.          if( i==0 )
  254.            {
  255.             fat[0]=0x0FFFFFF8;
  256.             fat[1]=0x0FFFFFFF;
  257.             fat[2]=0x0FFFFFFF; /* root */
  258.             while(next_bad!=num_bad && bbt[next_bad]<base_sect+clust_size*125)
  259.               fat[ (bbt[next_bad++]-base_sect)/clust_size+3 ]=0x0FFFFFF7;
  260.             base_sect+=clust_size*125;
  261.            }
  262.          else
  263.            {
  264.             while(next_bad!=num_bad && bbt[next_bad]<base_sect+clust_size*128)
  265.               fat[ (bbt[next_bad++]-base_sect)/clust_size ]=0x0FFFFFF7;
  266.             base_sect+=clust_size*128;
  267.            }
  268.  
  269.          if( disk_write_rel(p,wr_sect++,fat,1)==-1 )
  270.            {
  271.             progress("Error writing FAT.");
  272.             goto failed;
  273.            }
  274.         }
  275.     }
  276.  
  277.  memset(fat,0,512);
  278.  
  279.  progress("~Writing root directory ...");
  280.  
  281.  for( i=0 ; i<clust_size ; i++ )
  282.   if( disk_write_rel(p,wr_sect++,fat,1)==-1 )
  283.     {
  284.      progress("Error writing root directory.");
  285.      goto failed;
  286.     }
  287.  
  288.  disk_unlock(dinfo.disk);
  289.  free(data_pool);
  290.  return OK;
  291.  
  292. failed:
  293.  
  294.  disk_unlock(dinfo.disk);
  295.  free(data_pool);
  296.  return FAILED;
  297. }/* format_fat32 */
  298.  
  299.  
  300. int print_fat32(struct part_long *p )
  301. {
  302.  int i;
  303.  char tmp1[20], tmp2[20];
  304.  struct boot_fat32 *b=(struct boot_fat32*)tmp;
  305.  
  306.  if( disk_read_rel(p,0,b,3)==-1 )
  307.    {
  308.     fprintf(stderr,"Error reading boot sector.\n");
  309.     return FAILED;
  310.    }
  311.  
  312.  printf("\n                  Boot Sector Data              Expected Value\n\n");
  313.  printf("                        System id:  %-.8s\n", b->sys_id );
  314.  printf("                      Sector size:  %-3d         512\n", b->sect_size );
  315.  printf("              Sectors per cluster:  %d\n", b->clust_size );
  316.  printf("Reserved sectors at the beginning:  %d\n", b->res_sects  );
  317.  printf("             Number of FAT copies:  %d\n", b->fat_copies );
  318.  printf("            Media descriptor byte:  %02Xh         F8h\n", b->media_desc );
  319.  
  320.  printf("                     Drive number:  %-3d         %d\n", b->drive_num, dinfo.disk );
  321.  printf("                  Number of sides:  %-3d         %d\n", b->num_sides, dinfo.num_heads );
  322.  printf("                 Sectors per side:  %-2d          %d\n", b->track_size, dinfo.num_sects );
  323.  
  324.  printf("Hidden sectors prior to partition:  %-10s  %-10s\n", sprintf_long(tmp1,b->hid_sects), sprintf_long(tmp2,p->rel_sect) );
  325.  printf("          Total number of sectors:  %-10s  %-10s\n", sprintf_long(tmp1,b->num_sects), sprintf_long(tmp2,p->num_sect) );
  326.  printf("                  Sectors per FAT:  %u\n", b->sfat_size );
  327.  printf("              Big Sectors per FAT:  %lu\n", b->fat_size );
  328.  printf("             FAT Attributes (???):  %02Xh\n", b->fat_attr );
  329.  printf("            First cluster in root:  %lu\n", b->root_clust );
  330.  printf("                    Free clusters:  %lu\n", b->free_clust );
  331.  printf("                        Next free:  %lu\n", b->next_free );
  332.  
  333.  printf("   Extended boot record signature:  %02Xh\n", b->ext_signat );
  334.  printf("             Volume serial number:  %08lX\n", b->serial_num );
  335.  printf("                     Volume label:  %-.11s\n", b->label );
  336.  printf("                 File system type:  %-.8s    FAT32\n", b->fs_id );
  337.  printf("         Boot sector magic number:  0x%08lX  0xAA550000\n", b->magic_num );
  338.  
  339. #if 0
  340.      unsigned char  fs_ver_maj;    /* File System Version (major)        */
  341.      unsigned short fs_ver_min    /* File System Version (minor)        */
  342.  
  343.      unsigned short fs_sect_num; /* FS Sector number (1) ???        */
  344.      unsigned short bs_bak_sect; /* Boot sector backup (6)        */
  345.  
  346.      unsigned long  ext_sign2;     /* Ext Boot Record Sign (0x41615252)    */
  347.      unsigned long  ext_sign3;     /* FS Info Signature    (0x61417272)    */
  348.  
  349. #endif
  350.  
  351.  printf("  Second Boot sector magic number:  0x%08lX  0xAA550000\n", b->magic_num2 );
  352.  printf("   Third Boot sector magic number:  0x%08lX  0xAA550000\n", b->magic_num3 );
  353.  
  354.  return 0;
  355. }/* print_fat32 */
  356.  
  357. #define StX  5
  358. #define StY  5
  359. #define StW  66
  360. #define StH  14
  361.  
  362. #define StX2 (StX+30)
  363. #define StX3 (StX+69)
  364.  
  365. #define TEXT_COLOR (BrCyan+BakBlue)
  366. #define DATA_COLOR (BrWhite+BakBlue)
  367. #define EDIT_COLOR (BrWhite+BakBlack)
  368.  
  369. int setup_fat32(struct part_long *p)
  370. {
  371.  struct event ev;
  372.  int i, v, act, pos;
  373.  char *tmp, *tmp1, *tmp2;
  374.  struct boot_fat32 *b, *b_orig;
  375.  unsigned long n, l, lc, max_clust, min_clust, min_num_sect, max_num_sect, fatsz;
  376.  struct part_long *q;
  377.  
  378.  if( (tmp=malloc(7*SECT_SIZE))==0 )
  379.    {
  380.     show_error(ERROR_MALLOC);
  381.     return FAILED;
  382.    }
  383.  
  384.  tmp1=tmp+100;
  385. /* tmp2=tmp+120; */
  386.  b=(struct boot_fat32*)(tmp+SECT_SIZE);
  387.  b_orig=b+1;
  388.  
  389.  if( disk_read_rel(p,0,b,3)==-1 )
  390.    {
  391.     show_error("Error reading boot sector");
  392.     free(tmp);
  393.     return FAILED;
  394.    }
  395.  
  396.  memmove(b_orig,b,3*SECT_SIZE);
  397.  
  398.  clear_window(TEXT_COLOR,2,5,78,19);
  399.  write_string( HINT_COLOR, 56, 24,"F5");
  400.  write_string( MENU_COLOR, 58, 24," - SetExp");
  401.  
  402.  max_clust = ( (long)SECT_SIZE*8*b->fat_size/32 - 2 );
  403.  
  404.  write_string( TEXT_COLOR, StX, StY+ 1,"                  System id:                             FS Version:");
  405.  write_string( TEXT_COLOR, StX, StY+ 2,"           File system type:                             Media desc:");
  406.  write_string( TEXT_COLOR, StX, StY+ 3,"        Sectors per cluster:                            Sector size:");
  407.  write_string( TEXT_COLOR, StX, StY+ 4,"            Sectors per FAT:                             FAT copies:");
  408.  write_string( TEXT_COLOR, StX, StY+ 5,"      First cluster in Root:                               Ext sign:");
  409.  write_string( TEXT_COLOR, StX, StY+ 6,"       Volume serial number:                             Magic:");
  410.  write_string( TEXT_COLOR, StX, StY+ 7,"               Volume label:               Expected Value");
  411.  write_string( TEXT_COLOR, StX, StY+ 8,"               Drive number:");
  412.  write_string( TEXT_COLOR, StX, StY+ 9,"            Number of sides:");
  413.  write_string( TEXT_COLOR, StX, StY+10,"           Sectors per side:");
  414.  write_string( TEXT_COLOR, StX, StY+11," Sectors prior to partition:               123456789");
  415.  write_string( TEXT_COLOR, StX, StY+12,"    Total number of sectors:               123456789");
  416.  write_string( TEXT_COLOR, StX, StY+15,"     Minimum partition size:");
  417.  write_string( TEXT_COLOR, StX, StY+16,"     Current partition size:");
  418.  write_string( TEXT_COLOR, StX, StY+17,"     Maximum partition size:");
  419.  
  420.  sprintf(tmp,"%-.8s", b->sys_id );
  421.  write_string( DATA_COLOR, StX2, StY+1, tmp );
  422.  
  423.  sprintf(tmp,"%d.%02d", b->fs_ver_maj, b->fs_ver_min );
  424.  write_string( DATA_COLOR, StX3, StY+1, tmp );
  425.  
  426.  if( b->fs_ver_maj>0 || b->fs_ver_maj==0 && b->fs_ver_min>0 )
  427.    {
  428.     show_error("Warning: I wasn't tested with this FileSystem Version!!!");
  429.    }
  430.  
  431.  sprintf(tmp,"%-.8s", b->fs_id );
  432.  write_string( DATA_COLOR, StX2, StY+2, tmp );
  433.  
  434.  sprintf(tmp,"%Xh", b->media_desc );
  435.  write_string( DATA_COLOR, StX3, StY+2, tmp );
  436.  
  437.  sprintf(tmp,"%-5u %s bytes/clust", b->clust_size, sprintf_long(tmp1,(unsigned long)b->clust_size*SECT_SIZE) );
  438.  write_string( DATA_COLOR, StX2, StY+3, tmp );
  439.  
  440.  sprintf(tmp,"%d", b->sect_size );
  441.  write_string( DATA_COLOR, StX3, StY+3, tmp );
  442.  
  443.  sprintf(tmp,"%-5lu %s max clust", b->fat_size, sprintf_long(tmp1,max_clust) );
  444.  write_string( DATA_COLOR, StX2, StY+4, tmp );
  445.  
  446.  sprintf(tmp,"%d", b->fat_copies );
  447.  write_string( DATA_COLOR, StX3, StY+4, tmp );
  448.  
  449.  sprintf(tmp,"%lu", b->root_clust );
  450.  write_string( DATA_COLOR, StX2, StY+5, tmp );
  451.  
  452.  sprintf(tmp,"%Xh", b->ext_signat );
  453.  write_string( DATA_COLOR, StX3, StY+5, tmp );
  454.  
  455.  sprintf(tmp,"%08lX", b->serial_num );
  456.  write_string( DATA_COLOR, StX2, StY+6, tmp );
  457.  
  458.  
  459.  sprintf(tmp," %u", dinfo.disk );
  460.  write_string( DATA_COLOR, StX2+12, StY+ 8, tmp );
  461.  
  462.  sprintf(tmp," %u", dinfo.num_heads );
  463.  write_string( DATA_COLOR, StX2+12, StY+ 9, tmp );
  464.  
  465.  sprintf(tmp," %u", dinfo.num_sects );
  466.  write_string( DATA_COLOR, StX2+12, StY+10, tmp );
  467.  
  468.  sprintf(tmp," %-9lu", p->rel_sect );
  469.  write_string( DATA_COLOR, StX2+12, StY+11, tmp );
  470.  
  471.  sprintf(tmp," %-9lu", p->num_sect );
  472.  write_string( DATA_COLOR, StX2+12, StY+12, tmp );
  473.  
  474.  write_string( HINT_COLOR, StX2, StY+16, "Reading FAT... " );
  475.  move_cursor( StX2+21, StY+16 );
  476.  
  477.  fatsz=min(65536L,b->fat_size);
  478.  l=lc=0;
  479.  for( n=b->res_sects ; n<b->res_sects+fatsz ; n++ )
  480.   if( disk_read_rel(p,n,tmp,1)==-1 )
  481.     {
  482.      show_error("Error reading FAT table");
  483.      break;
  484.     }
  485.   else
  486.     {
  487.      write_int( HINT_COLOR, StX2+16, StY+16, 5, n-b->res_sects );
  488.      for( i=0 ; i<SECT_SIZE ; i++, l++ )
  489.       if( tmp[i]!=0 ) lc=l;
  490.     }
  491.  
  492.  min_clust = ( lc*8/32 + (lc*8%32==0?0:1) - 2);
  493.  
  494.  n = b->res_sects + b->fat_copies*b->fat_size;
  495.  
  496.  min_num_sect = n+min_clust*b->clust_size;
  497.  max_num_sect = n+max_clust*b->clust_size;
  498.  
  499.  sprintf(tmp,"%ld sectors = %s kbytes", min_num_sect, sprintf_long(tmp1,(min_num_sect)/2) );
  500.  write_string( DATA_COLOR, StX2, StY+15, tmp );
  501.  
  502.  sprintf(tmp,"%ld sectors = %s kbytes", max_num_sect, sprintf_long(tmp1,(max_num_sect)/2) );
  503.  write_string( DATA_COLOR, StX2, StY+17, tmp );
  504.  
  505.      n = b->num_sects;
  506.      sprintf(tmp,"%ld sectors = %s kbytes", n, sprintf_long(tmp1,n/2) );
  507.      write_string( DATA_COLOR, StX2, StY+16, tmp );
  508.  
  509.  pos=0;
  510.  act=0;
  511.  
  512.  while(1)
  513.     {
  514.      for( i=0 ; i<11 ; i++ ) tmp[i]=(b->label[i]==0)?' ':b->label[i];
  515.      tmp[i]=0;
  516.      write_string( DATA_COLOR, StX2, StY+ 7, tmp );
  517.      sprintf(tmp,"%-5d",b->drive_num);
  518.      write_string( (b->drive_num==dinfo.disk)?DATA_COLOR:INVAL_COLOR, StX2, StY+ 8, tmp );
  519.      sprintf(tmp,"%-5d",b->num_sides);
  520.      write_string( (b->num_sides==dinfo.num_heads)?DATA_COLOR:INVAL_COLOR, StX2, StY+ 9, tmp );
  521.      sprintf(tmp,"%-5d",b->track_size);
  522.      write_string( (b->track_size==dinfo.num_sects)?DATA_COLOR:INVAL_COLOR, StX2, StY+10, tmp );
  523.      sprintf(tmp,"%-9lu",b->hid_sects);
  524.      write_string( (b->hid_sects==p->rel_sect)?DATA_COLOR:INVAL_COLOR, StX2, StY+11, tmp );
  525.      sprintf(tmp,"%-9lu",b->num_sects);
  526.      write_string( (b->num_sects==p->num_sect)?DATA_COLOR:INVAL_COLOR, StX2, StY+12, tmp );
  527.      sprintf(tmp,"%08lX", b->magic_num );
  528.      write_string( (b->magic_num==MBR_MAGIC_NUM32)?DATA_COLOR:INVAL_COLOR, StX3-5, StY+6, tmp );
  529.  
  530.      p->changed = ( memcmp(b, b_orig, SECT_SIZE)==0 ) ? 0 : 1;
  531.      if( p->changed==0 ) write_string(         HINT_COLOR, 15, 24,"F2");
  532.      if( p->changed==1 ) write_string( Blink + HINT_COLOR, 15, 24,"F2");
  533.  
  534.      if( act==0 )
  535.        {
  536.         memmove(tmp,b->label,11);
  537.         tmp[11]=0;
  538.         for( i=10 ; i>=0 && tmp[i]==' ' ; i-- ) tmp[i]=0;
  539.         memmove(tmp1,tmp,12);
  540.         edit_str_field(&ev,0,EDIT_COLOR, StX2, StY+7, 12, tmp, &pos );
  541.         if( memcmp(tmp,tmp1,12)!=0 )
  542.           {
  543.            for( i=0 ; tmp[i]!=0 && i<11 ; i++ ) b->label[i]=tmp[i];
  544.            for( ; i<11 ; i++ ) b->label[i]=' ';
  545.           }
  546.        }
  547.  
  548.      if( act==1 )
  549.        {
  550.         n=b->drive_num;
  551.         edit_int_field(&ev,0,EDIT_COLOR, StX2, StY+ 8, -5, &n, 255L );
  552.         b->drive_num=n;
  553.        }
  554.  
  555.      if( act==2 )
  556.        {
  557.         n=b->num_sides;
  558.         edit_int_field(&ev,0,EDIT_COLOR, StX2, StY+ 9, -5, &n, 255L );
  559.         b->num_sides=n;
  560.        }
  561.  
  562.      if( act==3 )
  563.        {
  564.         n=b->track_size;
  565.         edit_int_field(&ev,0,EDIT_COLOR, StX2, StY+10, -5, &n, 63L );
  566.         b->track_size=n;
  567.        }
  568.  
  569.      if( act==4 )
  570.         edit_int_field(&ev,0,EDIT_COLOR, StX2, StY+11, -9,
  571.                                                 &b->hid_sects, 999999999L);
  572.      if( act==5 )
  573.         edit_int_field(&ev,0,EDIT_COLOR, StX2, StY+12, -9,
  574.                                                 &b->num_sects, 999999999L);
  575. /*   get_event(&ev,EV_KEY); */
  576.  
  577.      n = b->num_sects;
  578.      sprintf(tmp,"%ld sectors = %s kbytes", n, sprintf_long(tmp1,n/2) );
  579.      clear_window(DATA_COLOR, StX2, StY+16, 78-StX2, 1 );
  580.      write_string( (n>=min_num_sect && n<=max_num_sect)?DATA_COLOR:INVAL_COLOR, StX2, StY+16, tmp );
  581.  
  582.      p->changed = ( memcmp(b, b_orig, SECT_SIZE)==0 ) ? 0 : 1;
  583.      if( p->changed==0 ) write_string(         HINT_COLOR, 15, 24,"F2");
  584.      if( p->changed==1 ) write_string( Blink + HINT_COLOR, 15, 24,"F2");
  585.  
  586.      if( ev.key==27 )                 /* ESC */
  587.        {
  588.         if( p->changed ) continue;
  589.         break;
  590.        }
  591.      if( ev.key==13 || ev.key==9 )        /* Enter or Tab */
  592.        {
  593.         act=(act+1)%6;
  594.        }
  595.      else if( ev.scan==0x3B00 )                /* F1 - Help */
  596.        {
  597.         sprintf(tmp,"#setup_fat");
  598.         html_view(tmp);
  599.        }
  600.      else if( ev.scan==0x50E0 || ev.scan==0x5000 )    /* down */
  601.        {
  602.         if( act<5 ) act++;
  603.        }
  604.      else if( ev.scan==0x48E0 || ev.scan==0x4800 )    /* up */
  605.        {
  606.         if( act>0 ) act--;
  607.        }
  608.      else if( ev.scan==0x3C00  )                   /* F2 - Save */
  609.        {
  610.         if( p->inh_invalid || p->inh_changed )
  611.           { show_error(ERROR_INH_INVAL); continue; }
  612.  
  613.         flush_caches();
  614.  
  615.         disk_lock(dinfo.disk);
  616.         
  617.         if( disk_write_rel(p,0,b,3)==-1 ||
  618.             disk_write_rel(p,6,b,3)==-1   )
  619.           {
  620.            show_error("Error saving boot sector");
  621.           }
  622.         else
  623.           {
  624.            memmove(b_orig, b, 3*SECT_SIZE);
  625.           }
  626.  
  627.         disk_unlock(dinfo.disk);
  628.        }
  629.      else if( ev.scan==0x3D00  )                   /* F3 - Undo */
  630.        {
  631.         memmove( b, b_orig, 3*SECT_SIZE );
  632.        }
  633.      else if( ev.scan==0x3F00  )            /* F5 - Set */
  634.        {
  635.         b->drive_num=dinfo.disk;
  636.         b->num_sides=dinfo.num_heads;
  637.         b->num_sects=dinfo.num_sects;
  638.         b->hid_sects=p->rel_sect;
  639.         b->num_sects=p->num_sect;
  640.         b->magic_num=MBR_MAGIC_NUM32;
  641.        }
  642.  
  643.     }/* while(1) */
  644.  
  645.  free(tmp);
  646.  return OK;
  647. }/* setup_fat32 */
  648.