aboutsummaryrefslogtreecommitdiff
path: root/jindroush/lib/cfs_kboo.cpp
blob: 526e9a6e74733458a186659f024e302036125e7d (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
//    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 "cfs_kboo.h"
#include "autil.h"

CKBoot::CKBoot() : CFs()
{
	#ifdef _MEMORY_DUMP_
		printf( "CKBoot constructed: %08X\n", this );
	#endif
}

CKBoot::~CKBoot()
{
	Dismount();
	#ifdef _MEMORY_DUMP_
		printf( "CKBoot destructed: %08X\n", this );
	#endif
}

BOOL CKBoot::Mount( ADisk* pDisk )
{
	m_iFilesValid = 0;
	m_iFilesInvalid = 0;
	m_pDisk = pDisk;
	m_pRoot = NULL;

	if ( m_pDisk->GetSectorSize() != 0x80 )
	{
		sprintf( m_szLastError, "KBOOT: Can't process DD disk!" );
		return FALSE;
	}

	m_pRoot = CreateEntry();

	return m_pRoot ? TRUE : FALSE;
}

void CKBoot::Dismount()
{
	DeleteList( m_pRoot );
}

CKBootDirEntry* CKBoot::CreateEntry()
{
	CKBootDirEntry* pE = new CKBootDirEntry();

	if ( !pE )
		return NULL;

	BYTE abtSec[ 0x100 ];

	if ( !m_pDisk->ReadSector( abtSec, 1 ) )
	{
		sprintf( m_szLastError, "KBOOT: Can't read boot sector because\n%s", m_pDisk->GetLastError() );
		delete pE;
		return NULL;
	}

	pE->m_dwFileLen = abtSec[ 9 ] + ( abtSec[ 10 ] << 8 ) + ( abtSec[ 11 ] << 16 );

	if ( !m_pDisk->ReadSector( abtSec, 4 ) )
	{
		sprintf( m_szLastError, "KBOOT: Can't read first post boot sector because\n%s", m_pDisk->GetLastError() );
		delete pE;
		return NULL;
	}

	if ( ( abtSec[ 0 ] != 0xFF ) || ( abtSec[ 1 ] != 0xFF ) )
	{
		sprintf( m_szLastError, "KBOOT: Not a KBoot image!" );
		delete pE;
		return NULL;
	}

	int iSectors = 3 + ( ( pE->m_dwFileLen + 0x7F ) / 0x80 );

	if ( iSectors > m_pDisk->GetSectorCount() )
	{
		sprintf( m_szLastError, "KBOOT: Not a KBoot image or invalid! (%d<>%d)", iSectors, m_pDisk->GetSectorCount() );
		delete pE;
		return NULL;
	}

	sprintf( pE->m_szAscData, "%06lX", pE->m_dwFileLen );

	GuessBestFnameFromPC( pE->m_szFname, m_pDisk->GetImageName(), "axe" );

	return pE;
}

BOOL CKBoot::ExportFile( char* szOutFile, CDirEntry* pDirE )
{
	int hOutfile = -1;

	if ( szOutFile )
	{
		hOutfile = open( szOutFile, O_BINARY | O_CREAT | O_TRUNC | O_WRONLY, 0666 );

		if ( -1 == hOutfile )
		{
			sprintf( m_szLastError, "KBOOT: Unable to create file '%s'!", szOutFile );
			return FALSE;
		}
	}

	DWORD dwFileLen = ((CKBootDirEntry*)pDirE) ->m_dwFileLen;

	BYTE abtBuff[ 0x80 ];

	int iSector = 4;

	while( dwFileLen )
	{
		WORD wToCopy = ( dwFileLen < 0x80 ) ? dwFileLen : 0x80;

		if ( !m_pDisk->ReadSector( abtBuff, iSector ) )
		{
			sprintf( m_szLastError, "KBOOT: Can't read sector because\n%s", m_pDisk->GetLastError() );
			return FALSE;
		}

		if ( -1 != hOutfile )
			write( hOutfile, abtBuff, wToCopy );

		dwFileLen -= wToCopy;
		iSector++;

	}
	if ( -1 != hOutfile )
		close( hOutfile );

	return TRUE;
}