		.386
		.model flat,stdcall
		option casemap:none 

		include \masm32\include\windows.inc
		include \masm32\include\user32.inc
		include \masm32\include\kernel32.inc
		include \masm32\macros\Strings.mac

		includelib \masm32\lib\user32.lib
		includelib \masm32\lib\kernel32.lib
	
		.data

root_done_flag	db	1
up_dir		db	'..', 0


romdir	struct					; Thx to pcsx2's source
	fileName	BYTE	10 dup (?)
	extInfoSize	WORD	?
	fileSize	DWORD	?
romdir	ends


		.data?

bios_fhandle	dd	?
bios_fsize	dd	?
bios_maphandle	dd	?
bios_mapbase	dd	?

module_fsize	dd	?
module_fhandle	dd	?
module_base	dd	?

sub_module_base	dd	?
sub_module_fsize	dd	?

temp		dd	?


		.code
_start:
		invoke	GetCommandLine
		
		mov	EDI, EAX
		xor	AL, AL
		mov	ECX, 100h		
		cld
		repnz	scasb
		mov	AL,20h
		std
		repnz	scasb
		cld
		inc	EDI
		inc	EDI

		invoke	CreateFileA,		\
			EDI,			\
			GENERIC_READ,		\
			NULL,			\
			NULL,			\
			OPEN_EXISTING,		\
			NULL,			\
			NULL

		mov	bios_fhandle, EAX

		.IF	EAX == INVALID_HANDLE_VALUE
		
			invoke	MessageBox, 									\
				NULL,										\
				$CTA0("BIOS image file not found"),						\
				$CTA0("Copyright (C) 2005 by Robert Ayrapetyan [robert@k-medulla.com]"),	\
				MB_OK or MB_ICONERROR
			jmp	_fin
		.ENDIF

		invoke	GetFileSize,		\
			bios_fhandle,		\
			NULL

		sub	EAX, 10h
		mov	bios_fsize, EAX
		
		invoke	CreateFileMapping,	\
			bios_fhandle,		\
			NULL,			\
			PAGE_READONLY,		\
			NULL,			\
			NULL,			\
			NULL
		
		mov	bios_maphandle, EAX

		invoke	MapViewOfFile,		\
			bios_maphandle,		\
			FILE_MAP_READ,		\
			NULL,			\
			NULL,			\
			NULL

		mov	bios_mapbase, EAX
		mov	module_base, EAX
		add	bios_fsize, EAX

		mov	EDI, bios_mapbase
_find_reset:
		mov	EAX, [EDI]
		cmp	EAX, 'ESER'
		jz	_romdir_found
		add	EDI, 10h		
		cmp	EDI, bios_fsize
		jl	_find_reset

		invoke	MessageBox, 									\
			NULL,										\
			$CTA0("Invalid BIOS image file"),						\
			$CTA0("Copyright (C) 2005 by Robert Ayrapetyan [robert@k-medulla.com]"),	\
			MB_OK or MB_ICONERROR
		jmp	_fin

_romdir_found:		
		assume	EDI: PTR romdir
_next_module:
		

		
		mov	EAX, dword ptr [module_base]
		mov	EAX, [EAX]
		cmp	EAX, 'ESER'
		jnz	_no_subfolder

		dec	root_done_flag
		jz	_no_subfolder

		invoke	CreateDirectory,	\
			addr [EDI].fileName,	\
			NULL

		invoke	SetCurrentDirectory, 	\
			addr [EDI].fileName
			
		push	EDI
		mov	EDI, module_base				
		mov	sub_module_base, EDI

_next_module2:
		mov	EAX, dword ptr [EDI].fileName
		test	EAX, EAX
		jz	_last_file_in_folder

;************************************************************
		mov	EAX, [EDI].fileSize
		test    AL, 0Fh
		jz      _para_aligned2
		add     EAX, 10h
		and     EAX, 0FFFFFFF0h
_para_aligned2:
		mov	sub_module_fsize, EAX
		
		invoke	CreateFileA,		\
			addr [EDI].fileName,	\
			GENERIC_WRITE,		\
			NULL,			\
			NULL,			\
			CREATE_ALWAYS,		\
			NULL,			\
			NULL

		mov	module_fhandle, EAX

		invoke 	WriteFile, 		\
			module_fhandle,		\
			sub_module_base,	\
			sub_module_fsize,	\
			addr temp,		\
			NULL

		invoke	CloseHandle,		\
			module_fhandle

		mov	EAX, sub_module_fsize
		add	sub_module_base, EAX
		add	EDI, 10h
		jmp	_next_module2
;*********************************************
		




_last_file_in_folder:	
		pop	EDI	
		invoke	SetCurrentDirectory, 	\
			addr up_dir

		mov	EAX, [EDI].fileSize
		test    AL, 0Fh
		jz      _para_aligned3
		add     EAX, 10h
		and     EAX, 0FFFFFFF0h
_para_aligned3:
		mov	module_fsize, EAX

		jmp	_M1

		
_no_subfolder:
		mov	EAX, dword ptr [EDI].fileName
		test	EAX, EAX
		jz	_fin_ok

		mov	EAX, [EDI].fileSize
		test    AL, 0Fh
		jz      _para_aligned
		add     EAX, 10h
		and     EAX, 0FFFFFFF0h
_para_aligned:
		mov	module_fsize, EAX
		
		invoke	CreateFileA,		\
			addr [EDI].fileName,	\
			GENERIC_WRITE,		\
			NULL,			\
			NULL,			\
			CREATE_ALWAYS,		\
			NULL,			\
			NULL

		mov	module_fhandle, EAX

		invoke 	WriteFile, 		\
			module_fhandle,		\
			module_base,		\
			module_fsize,		\
			addr temp,		\
			NULL

		invoke	CloseHandle,		\
			module_fhandle

_M1:	
		mov	EAX, module_fsize
		add	module_base, EAX
		add	EDI, 10h
		jmp	_next_module
_fin_ok:
		invoke	UnmapViewOfFile,	\
			bios_mapbase
		
		invoke 	CloseHandle,		\
			bios_maphandle

		invoke	CloseHandle,		\
			bios_fhandle		

		invoke	MessageBox, 									\
			NULL,										\
			$CTA0("Done\:"),								\
			$CTA0("Copyright (C) 2005 by Robert Ayrapetyan [robert@k-medulla.com]"),	\
			MB_OK or MB_ICONINFORMATION
_fin:
		invoke	ExitProcess,		\
			NULL

		end	_start