makefat.c 2.67 KB
/*
 * makefat.c
 *
 *     ファイルサイズからfatデータの作成。
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "sub.h"
/* extern	int tab[][4292]; */

#ifndef null 
#define null 0
#endif

/* char 	NULSTR[1]={0}; */
FILE *fp;

char * buf[32];
extern int lba2Byte(int type, int startLBA, int nlbas);
extern int byte2LBA(int type, int startLBA, int nlbas);

u8 usage[] = "makefat [-h] [link map file] [bootseg name] \n";
u8 default_bootSegName[] = "code";
u8 default_outfile[] = "fat";

/* local functions */
void 	myErrStopSSI(char * str ,char * s1 ,char * s2 ,int i ,int stop);
s32     myStr2Long(char * str);
s32     _getSizeOfFile(char * file);
FILE *  myOpen(char * filename , char * mode);
s32  rev32(u32 data);

/* implementation  */

int main(int argc, char **argv)
{
  s32 lbasize,ret,_from,_to;
  u8  *t1,*bootSegName,*outfile;
  u8  _cmd[255],_line[512],_head[255],_tail[255];

// 引数の処理と初期値設定

  sbGetOpts(argv,argc,"h0o:");

  if( !OPTIA[0] && TGTPYAS > 0)
  {
    t1 = TGTPYA[0];
	bootSegName = TGTPYA[1];
	outfile = (u8 *)OPTIA[1];

	if( bootSegName == NULSTR ) bootSegName = default_bootSegName;
	if( outfile == NULSTR ) outfile = default_outfile;

	sprintf(_head,"_%sSegmentDiskStart",bootSegName);
	sprintf(_tail,"_%sSegmentDiskEnd",bootSegName);
	_from = 0;
	_to = 0;
  }
  else
  {
	printf(usage);
    exit(0);
  }

// シンボル情報の取得
  
  sprintf(_cmd,"nm -Bvx %s | grep Disk > xxxMakefatTemp",t1);
  printf("リンクマップを調べています・・・\n");
  system(_cmd);

  fp = sbOpen( "xxxMakefatTemp" ,"r");
  while( fgets(_line,512,fp) != NULL)
	{
	  _0_(_line);
	  if( !_from )
		{
		  if( sbM1("^(.*)$s+.$s+%s",_head) != -1 )
			{
			  _from = strtol(_1,NULL,16);
			  continue;
			}
		}

	  if( sbM1("^(.*)$s+.$s+%s",_tail) != -1 )
		{
		  _to = strtol(_1,NULL,16);
		  break;
		}
	}
  sbClose(fp);

  if(_from && _to && _from<_to)
	{
	  sprintf(_cmd,"rm -f xxxMakefatTemp");
	  system(_cmd);
	}
  else
	{
	  es(_to,"fail to get DiskMap ");
	}

// ファイル出力

  lbasize = _to - _from;

  printf("(startlba,lbasize) = (%d,%d) \n",_from,lbasize);

  lbasize  = rev32(lbasize);
  fp = sbBinOpen(outfile,"w");
  ret = fwrite( &_from , sizeof(u8) , sizeof(s32) , fp );
  if( ret != sizeof(s32) ) sbErrStop("write failed");
  
  ret = fwrite( &lbasize  , sizeof(u8) , sizeof(s32) , fp );
  if( ret != sizeof(s32) ) sbErrStop("write failed");

  sbClose(fp);

  return 0;
} /* main */

s32 
rev32(u32 data)
{
#ifdef __PC__
    u8 c0;
#endif

    union {
	u32 ld;
	u8  ch[4];
    } d;

    d.ld=data;

#ifdef __PC__  /* エンディアンのリバース  */
    c0=d.ch[0];
    d.ch[0] = d.ch[3];
    d.ch[3] = c0;

    c0=d.ch[1];
    d.ch[1] = d.ch[2];
    d.ch[2] = c0;
#endif

    return d.ld;
}