aboutsummaryrefslogtreecommitdiff
path: root/jindroush/bas2boot/bas2boot.cpp
blob: bb8f2f85e14f8369139c6833f6ce79a4e8f4b38b (plain)
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
//    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 of the License, or
//    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 "pub.def"
#include "jintypes.h"
#include "autil.h"
#include "adsk.h"

DISK_TYPE g_dtypeOut = DISK_ATR;
BOOL g_bClassic = FALSE;
#define MAX_STRING 256
char g_szMessage[ MAX_STRING ] = "";

typedef struct
{
	WORD w1;
	WORD w2;
	WORD w3;
	WORD w4;
	WORD w5;
	WORD w6;
	WORD w7;
} BASHEAD;

#define BAS_HEAD_SIZE 0x0E

#define SHEADER PRG_NAME " v" PRG_VERSION " (c) " PRG_COPYRIGHT " " PRG_AUTHOR "\n"

#define HEADER SHEADER \
   PRG_DESC "\n" \
	"  Latest version can be found at " PRG_URL "\n" \
	"  Published under GPL. See GPL.TXT.\n\n"

#define USAGE HEADER "Usage:  " PRG_NAME " " PRG_USAGE

#include "switches.cpp"

int main( int argc, char* argv[] )
{
	setbuf( stdout, NULL );
	setbuf( stderr, NULL );

	if ( !SWITCHES_Init( &argc, argv ) )
		return 1;

	if ( argc < 2 )
	{
		SWFN_HELP( USAGE );
		return 1;
	}

	fprintf( stderr, SHEADER );

	char* szInFile = argv[ 1 ];
	char szOutFile[ MAX_STRING ];

	//setting the message
	char szMessage[ MAX_STRING ];
	strcpy( szMessage, g_szMessage );

	//if not present, set to 'Loading prgname'
	if ( ! *szMessage )
	{
		strcpy( szMessage, "Loading " );
		strcat( szMessage, szInFile );
	}

	//and pad it with spaces
	memset( szMessage + strlen( szMessage), 0x20, MAX_STRING - strlen( szMessage ) );
	szMessage[ MAX_STRING - 1 ] = '\0';

	//get output filename
	if ( argc >= 3 )
	{
		strcpy( szOutFile, argv[ 2 ] );
	}
	else
	{
		GuessBestFnameFromPC( szOutFile, szInFile, GetDiskTypeExt( g_dtypeOut ) );
	}

	ADisk* pDisk = NULL;
	switch( g_dtypeOut )
	{
		case DISK_ATR:
			pDisk = new CAtr();
			break;

		case DISK_XFD:
			pDisk = new CXfd();
			break;

		case DISK_SCP:
			pDisk = new CScp();
			break;

		case DISK_DCM:
			pDisk = new CDcm();
			break;

		case DISK_DI:
			pDisk = new CDi();
			break;

		default:
			break;
	}

	if ( !pDisk )
	{
		printf( "Can't create such image!\n" );
		return FALSE;
	}

	//reads boot stub
	char szBootName[ MAX_STRING ];
	strcpy( szBootName, argv[ 0 ] );
	char* szWhere = strrchr( szBootName, '/' );

	if ( szWhere )
		szWhere++;
	else
		szWhere = szBootName;
		
	strcpy( szWhere, "bas2boot.boo" );

	CFile cf;

	if ( !cf.Open( szBootName ) )
	{
		printf( "Can't open boot image: %s\n", szBootName );
		return 1;
	}

	long lBootLen = cf.GetLength();

	if ( lBootLen % 0x80 )
	{
		printf( "Invalid boot image: %s\n", szBootName );
		cf.Close();
		return 1;
	}

	BYTE* pbtBoot = new BYTE [ lBootLen ];

	cf.Read( pbtBoot, lBootLen );

	cf.Close();

	int iBootSectors = lBootLen / 0x80;

	if ( pbtBoot[ 1 ] != iBootSectors )
	{
		printf( "Invalid boot image: %s\nSize mismatch.\n", szBootName );
		cf.Close();
		delete [] pbtBoot;
		return 1;
	}

	//open basic file
	if ( !cf.Open( szInFile ) )
	{
		printf( "Can't open source file '%s'!\n", szInFile );
		return 1;
	}

	long lBasicLen = cf.GetLength();

	//set real length in header
	pbtBoot[ 8 ] = lBasicLen & 0xFF;
	pbtBoot[ 9 ] = lBasicLen >> 8;

	lBasicLen -= BAS_HEAD_SIZE;

	int iBasicSectors = ( lBasicLen + 0x7F ) / 0x80;

	//load and relocate basic header
	BASHEAD head;
	head.w1 = cf.readLEw();
	head.w2 = cf.readLEw() - head.w1;
	head.w3 = cf.readLEw() - head.w1;
	head.w4 = cf.readLEw() - head.w1;
	head.w5 = cf.readLEw() - head.w1;
	head.w6 = cf.readLEw() - head.w1;
	head.w7 = cf.readLEw() - head.w1;

	BYTE* pbtHd = pbtBoot + lBootLen - BAS_HEAD_SIZE - 2;

	*( pbtHd++ ) = ( iBasicSectors - 1 ) & 0xFF;
	*( pbtHd++ ) = ( iBasicSectors - 1 ) >>8;

	*( pbtHd++ ) = head.w1 & 0xFF;
	*( pbtHd++ ) = head.w1 >> 8;

	*( pbtHd++ ) = head.w2 & 0xFF;
	*( pbtHd++ ) = head.w2 >> 8;

	*( pbtHd++ ) = head.w3 & 0xFF;
	*( pbtHd++ ) = head.w3 >> 8;

	*( pbtHd++ ) = head.w4 & 0xFF;
	*( pbtHd++ ) = head.w4 >> 8;

	*( pbtHd++ ) = head.w5 & 0xFF;
	*( pbtHd++ ) = head.w5 >> 8;

	*( pbtHd++ ) = head.w6 & 0xFF;
	*( pbtHd++ ) = head.w6 >> 8;

	*( pbtHd++ ) = head.w7 & 0xFF;
	*( pbtHd++ ) = head.w7 >> 8;

	//find empty space at the end of boot
	BYTE* pbtString = pbtBoot + lBootLen - BAS_HEAD_SIZE - 2 - 2;
	BYTE* pbtStringEnd = pbtString;

	while( *pbtString == 0x20 )
		pbtString--;

	pbtString++;

	int iStringLen = pbtStringEnd - pbtString;

	//put the message there
	memcpy( pbtString, szMessage, iStringLen + 1 );

	//read basic program
	BYTE* pbtBas = new BYTE [ iBasicSectors * 0x80 ];
	memset( pbtBas, 0, iBasicSectors * 0x80 );

	cf.Read( pbtBas, lBasicLen );

	cf.Close();

	//format disk
	DISK_GEOMETRY dg;
	
	if ( g_bClassic )
		ForceClassicSize( iBasicSectors + iBootSectors, 0x80, &dg );
	else
		GuessClassicSizes( iBasicSectors + iBootSectors, 0x80, &dg );

	if ( !pDisk->Format( &dg ) )
	{
		delete pDisk;
		delete[] pbtBoot;
		delete [] pbtBas;
		return 1;
	}

	//write boot & body
	pDisk->WriteSectors( 1, pbtBoot, iBootSectors );
	pDisk->WriteSectors( 1 + iBootSectors, pbtBas, iBasicSectors );

	//save it to disk
	if ( !pDisk->Save( szOutFile, FALSE ) )
	{
		printf( "Error! %s\n", pDisk->GetLastError() );
		delete pDisk;
		delete [] pbtBoot;
		delete [] pbtBas;
		return 1;
	}

	delete pDisk;

	delete [] pbtBoot;
	delete [] pbtBas;

	printf( "Written to %s (%s)\n", szOutFile, GetDiskTypeName( g_dtypeOut ) );

	return 0;
}