1 /*
2  *  Copyright (C) 2002-2015  The DOSBox Team
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  *  Wengier: LFN and AUTO MOUNT support
19  */
20 
21 
22 #include "dosbox.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <string>
27 #include <vector>
28 #include "programs.h"
29 #include "support.h"
30 #include "drives.h"
31 #include "cross.h"
32 #include "regs.h"
33 #include "callback.h"
34 #include "cdrom.h"
35 #include "dos_system.h"
36 #include "dos_inc.h"
37 #include "bios.h"
38 #include "bios_disk.h"
39 #include "setup.h"
40 #include "control.h"
41 #include "inout.h"
42 #include "dma.h"
43 
44 
45 #if defined(OS2)
46 #define INCL DOSFILEMGR
47 #define INCL_DOSERRORS
48 #include "os2.h"
49 #endif
50 
51 #if defined(WIN32)
52 #ifndef S_ISDIR
53 #define S_ISDIR(m) (((m)&S_IFMT)==S_IFDIR)
54 #endif
55 #endif
56 
57 #if C_DEBUG
58 Bitu DEBUG_EnableDebugger(void);
59 #endif
60 
61 void MSCDEX_SetCDInterface(int intNr, int forceCD);
62 static Bitu ZDRIVE_NUM = 25;
63 
64 class MOUNT : public Program {
65 public:
ListMounts(void)66 	void ListMounts(void) {
67 		char name[DOS_NAMELENGTH_ASCII],lname[LFN_NAMELENGTH];
68 		Bit32u size;Bit16u date;Bit16u time;Bit8u attr;
69 		/* Command uses dta so set it to our internal dta */
70 		RealPt save_dta = dos.dta();
71 		dos.dta(dos.tables.tempdta);
72 		DOS_DTA dta(dos.dta());
73 
74 		WriteOut(MSG_Get("PROGRAM_MOUNT_STATUS_1"));
75 		WriteOut(MSG_Get("PROGRAM_MOUNT_STATUS_FORMAT"),"Drive","Type","Label");
76 		for(int p = 0;p < 8;p++) WriteOut("----------");
77 
78 		for (int d = 0;d < DOS_DRIVES;d++) {
79 			if (!Drives[d]) continue;
80 
81 			char root[7] = {static_cast<char>('A'+d),':','\\','*','.','*',0};
82 			bool ret = DOS_FindFirst(root,DOS_ATTR_VOLUME);
83 			if (ret) {
84 				dta.GetResult(name,lname,size,date,time,attr);
85 				DOS_FindNext(); //Mark entry as invalid
86 			} else name[0] = 0;
87 
88 			/* Change 8.3 to 11.0 */
89 			char* dot = strchr(name,'.');
90 			if(dot && (dot - name == 8) ) {
91 				name[8] = name[9];name[9] = name[10];name[10] = name[11];name[11] = 0;
92 			}
93 
94 			root[1] = 0; //This way, the format string can be reused.
95 			WriteOut(MSG_Get("PROGRAM_MOUNT_STATUS_FORMAT"),root, Drives[d]->GetInfo(),name);
96 		}
97 		dos.dta(save_dta);
98 	}
99 
Run(void)100 	void Run(void) {
101 		DOS_Drive * newdrive;char drive;
102 		std::string label;
103 		std::string umount;
104 		std::string newz;
105 
106 		//Hack To allow long commandlines
107 		ChangeToLongCmd();
108 		/* Parse the command line */
109 		/* if the command line is empty show current mounts */
110 		if (!cmd->GetCount()) {
111 			ListMounts();
112 			return;
113 		}
114 
115 		/* In secure mode don't allow people to change mount points.
116 		 * Neither mount nor unmount */
117 		if(control->SecureMode()) {
118 			WriteOut(MSG_Get("PROGRAM_CONFIG_SECURE_DISALLOW"));
119 			return;
120 		}
121 		bool path_relative_to_last_config = false;
122 		if (cmd->FindExist("-pr",true)) path_relative_to_last_config = true;
123 
124 		/* Check for unmounting */
125 		if (cmd->FindString("-u",umount,false)) {
126 			umount[0] = toupper(umount[0]);
127 			int i_drive = umount[0]-'A';
128 				if (i_drive < DOS_DRIVES && i_drive >= 0 && Drives[i_drive]) {
129 					switch (DriveManager::UnmountDrive(i_drive)) {
130 					case 0:
131 						Drives[i_drive] = 0;
132 						if(i_drive == DOS_GetDefaultDrive())
133 							DOS_SetDrive(ZDRIVE_NUM);
134 						WriteOut(MSG_Get("PROGRAM_MOUNT_UMOUNT_SUCCESS"),umount[0]);
135 						break;
136 					case 1:
137 						WriteOut(MSG_Get("PROGRAM_MOUNT_UMOUNT_NO_VIRTUAL"));
138 						break;
139 					case 2:
140 						WriteOut(MSG_Get("MSCDEX_ERROR_MULTIPLE_CDROMS"));
141 						break;
142 					}
143 				} else {
144 					WriteOut(MSG_Get("PROGRAM_MOUNT_UMOUNT_NOT_MOUNTED"),umount[0]);
145 				}
146 			return;
147 		}
148 
149 		/* Check for moving Z: */
150 		/* Only allowing moving it once. It is merely a convenience added for the wine team */
151 		if (ZDRIVE_NUM == 25 && cmd->FindString("-z", newz,false)) {
152 			newz[0] = toupper(newz[0]);
153 			int i_newz = newz[0] - 'A';
154 			if (i_newz >= 0 && i_newz < DOS_DRIVES-1 && !Drives[i_newz]) {
155 				ZDRIVE_NUM = i_newz;
156 				/* remap drives */
157 				Drives[i_newz] = Drives[25];
158 				Drives[25] = 0;
159 				DOS_Shell *fs = static_cast<DOS_Shell *>(first_shell); //dynamic ?
160 				/* Update environment */
161 				std::string line = "";
162 				char ppp[2] = {newz[0],0};
163 				std::string tempenv = ppp; tempenv += ":\\";
164 				if (fs->GetEnvStr("PATH",line)){
165 					std::string::size_type idx = line.find('=');
166 					std::string value = line.substr(idx +1 , std::string::npos);
167 					while ( (idx = value.find("Z:\\")) != std::string::npos ||
168 					        (idx = value.find("z:\\")) != std::string::npos  )
169 						value.replace(idx,3,tempenv);
170 					line = value;
171 				}
172 				if (!line.size()) line = tempenv;
173 				fs->SetEnv("PATH",line.c_str());
174 				tempenv += "COMMAND.COM";
175 				fs->SetEnv("COMSPEC",tempenv.c_str());
176 
177 				/* Update batch file if running from Z: (very likely: autoexec) */
178 				if(fs->bf) {
179 					std::string &name = fs->bf->filename;
180 					if(name.length() >2 &&  name[0] == 'Z' && name[1] == ':') name[0] = newz[0];
181 				}
182 				/* Change the active drive */
183 				if (DOS_GetDefaultDrive() == 25) DOS_SetDrive(i_newz);
184 			}
185 			return;
186 		}
187 		/* Show list of cdroms */
188 		if (cmd->FindExist("-cd",false)) {
189 			int num = SDL_CDNumDrives();
190    			WriteOut(MSG_Get("PROGRAM_MOUNT_CDROMS_FOUND"),num);
191 			for (int i=0; i<num; i++) {
192 				WriteOut("%2d. %s\n",i,SDL_CDName(i));
193 			};
194 			return;
195 		}
196 
197 		std::string type="dir";
198 		cmd->FindString("-t",type,true);
199 		bool iscdrom = (type =="cdrom"); //Used for mscdex bug cdrom label name emulation
200 		if (type=="floppy" || type=="dir" || type=="cdrom") {
201 			Bit16u sizes[4];
202 			Bit8u mediaid;
203 			std::string str_size;
204 			if (type=="floppy") {
205 				str_size="512,1,2880,2880";/* All space free */
206 				mediaid=0xF0;		/* Floppy 1.44 media */
207 			} else if (type=="dir") {
208 				// 512*32*32765==~500MB total size
209 				// 512*32*16000==~250MB total free size
210 				str_size="512,32,32765,16000";
211 				mediaid=0xF8;		/* Hard Disk */
212 			} else if (type=="cdrom") {
213 				str_size="2048,1,65535,0";
214 				mediaid=0xF8;		/* Hard Disk */
215 			} else {
216 				WriteOut(MSG_Get("PROGAM_MOUNT_ILL_TYPE"),type.c_str());
217 				return;
218 			}
219 			/* Parse the free space in mb's (kb's for floppies) */
220 			std::string mb_size;
221 			if(cmd->FindString("-freesize",mb_size,true)) {
222 				char teststr[1024];
223 				Bit16u freesize = static_cast<Bit16u>(atoi(mb_size.c_str()));
224 				if (type=="floppy") {
225 					// freesize in kb
226 					sprintf(teststr,"512,1,2880,%d",freesize*1024/(512*1));
227 				} else {
228 					Bit32u total_size_cyl=32765;
229 					Bit32u free_size_cyl=(Bit32u)freesize*1024*1024/(512*32);
230 					if (free_size_cyl>65534) free_size_cyl=65534;
231 					if (total_size_cyl<free_size_cyl) total_size_cyl=free_size_cyl+10;
232 					if (total_size_cyl>65534) total_size_cyl=65534;
233 					sprintf(teststr,"512,32,%d,%d",total_size_cyl,free_size_cyl);
234 				}
235 				str_size=teststr;
236 			}
237 
238 			cmd->FindString("-size",str_size,true);
239 			char number[20];const char * scan=str_size.c_str();
240 			Bitu index=0;Bitu count=0;
241 			/* Parse the str_size string */
242 			while (*scan) {
243 				if (*scan==',') {
244 					number[index]=0;sizes[count++]=atoi(number);
245 					index=0;
246 				} else number[index++]=*scan;
247 				scan++;
248 			}
249 			number[index]=0;sizes[count++]=atoi(number);
250 
251 			// get the drive letter
252 			cmd->FindCommand(1,temp_line);
253 			if ((temp_line.size() > 2) || ((temp_line.size()>1) && (temp_line[1]!=':'))) goto showusage;
254 			drive=toupper(temp_line[0]);
255 			if (!isalpha(drive)) goto showusage;
256 
257 			if (!cmd->FindCommand(2,temp_line)) goto showusage;
258 			if (!temp_line.size()) goto showusage;
259 			if(path_relative_to_last_config && control->configfiles.size() && !Cross::IsPathAbsolute(temp_line)) {
260 				std::string lastconfigdir(control->configfiles[control->configfiles.size()-1]);
261 				std::string::size_type pos = lastconfigdir.rfind(CROSS_FILESPLIT);
262 				if(pos == std::string::npos) pos = 0; //No directory then erase string
263 				lastconfigdir.erase(pos);
264 				if (lastconfigdir.length())	temp_line = lastconfigdir + CROSS_FILESPLIT + temp_line;
265 			}
266 			struct stat test;
267 			//Win32 : strip tailing backslashes
268 			//os2: some special drive check
269 			//rest: substitute ~ for home
270 			bool failed = false;
271 #if defined (WIN32) || defined(OS2)
272 			/* Removing trailing backslash if not root dir so stat will succeed */
273 			if(temp_line.size() > 3 && temp_line[temp_line.size()-1]=='\\') temp_line.erase(temp_line.size()-1,1);
274 			if (stat(temp_line.c_str(),&test)) {
275 #endif
276 #if defined(WIN32)
277 // Nothing to do here.
278 #elif defined (OS2)
279 				if (temp_line.size() <= 2) // Seems to be a drive.
280 				{
281 					failed = true;
282 					HFILE cdrom_fd = 0;
283 					ULONG ulAction = 0;
284 
285 					APIRET rc = DosOpen((unsigned char*)temp_line.c_str(), &cdrom_fd, &ulAction, 0L, FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS,
286 						OPEN_FLAGS_DASD | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY, 0L);
287 					DosClose(cdrom_fd);
288 					if (rc != NO_ERROR && rc != ERROR_NOT_READY)
289 					{
290 						failed = true;
291 					} else {
292 						failed = false;
293 					}
294 				}
295 			}
296 			if (failed) {
297 #else
298 			if (stat(temp_line.c_str(),&test)) {
299 				failed = true;
300 				Cross::ResolveHomedir(temp_line);
301 				//Try again after resolving ~
302 				if(!stat(temp_line.c_str(),&test)) failed = false;
303 			}
304 			if(failed) {
305 #endif
306 				WriteOut(MSG_Get("PROGRAM_MOUNT_ERROR_1"),temp_line.c_str());
307 				return;
308 			}
309 			/* Not a switch so a normal directory/file */
310 			if (!S_ISDIR(test.st_mode)) {
311 #ifdef OS2
312 				HFILE cdrom_fd = 0;
313 				ULONG ulAction = 0;
314 
315 				APIRET rc = DosOpen((unsigned char*)temp_line.c_str(), &cdrom_fd, &ulAction, 0L, FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS,
316 					OPEN_FLAGS_DASD | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY, 0L);
317 				DosClose(cdrom_fd);
318 				if (rc != NO_ERROR && rc != ERROR_NOT_READY) {
319 				WriteOut(MSG_Get("PROGRAM_MOUNT_ERROR_2"),temp_line.c_str());
320 				return;
321 			}
322 #else
323 				WriteOut(MSG_Get("PROGRAM_MOUNT_ERROR_2"),temp_line.c_str());
324 				return;
325 #endif
326 			}
327 
328 			if (temp_line[temp_line.size()-1]!=CROSS_FILESPLIT) temp_line+=CROSS_FILESPLIT;
329 			Bit8u bit8size=(Bit8u) sizes[1];
330 			if (type=="cdrom") {
331 				int num = -1;
332 				cmd->FindInt("-usecd",num,true);
333 				int error = 0;
334 				if (cmd->FindExist("-aspi",false)) {
335 					MSCDEX_SetCDInterface(CDROM_USE_ASPI, num);
336 				} else if (cmd->FindExist("-ioctl_dio",false)) {
337 					MSCDEX_SetCDInterface(CDROM_USE_IOCTL_DIO, num);
338 				} else if (cmd->FindExist("-ioctl_dx",false)) {
339 					MSCDEX_SetCDInterface(CDROM_USE_IOCTL_DX, num);
340 #if defined (WIN32)
341 				} else if (cmd->FindExist("-ioctl_mci",false)) {
342 					MSCDEX_SetCDInterface(CDROM_USE_IOCTL_MCI, num);
343 #endif
344 				} else if (cmd->FindExist("-noioctl",false)) {
345 					MSCDEX_SetCDInterface(CDROM_USE_SDL, num);
346 				} else {
347 #if defined (WIN32)
348 					// Check OS
349 					OSVERSIONINFO osi;
350 					osi.dwOSVersionInfoSize = sizeof(osi);
351 					GetVersionEx(&osi);
352 					if ((osi.dwPlatformId==VER_PLATFORM_WIN32_NT) && (osi.dwMajorVersion>5)) {
353 						// Vista/above
354 						MSCDEX_SetCDInterface(CDROM_USE_IOCTL_DX, num);
355 					} else {
356 						MSCDEX_SetCDInterface(CDROM_USE_IOCTL_DIO, num);
357 					}
358 #else
359 					MSCDEX_SetCDInterface(CDROM_USE_IOCTL_DIO, num);
360 #endif
361 				}
362 				newdrive  = new cdromDrive(drive,temp_line.c_str(),sizes[0],bit8size,sizes[2],0,mediaid,error);
363 				// Check Mscdex, if it worked out...
364 				switch (error) {
365 					case 0  :	WriteOut(MSG_Get("MSCDEX_SUCCESS"));				break;
366 					case 1  :	WriteOut(MSG_Get("MSCDEX_ERROR_MULTIPLE_CDROMS"));	break;
367 					case 2  :	WriteOut(MSG_Get("MSCDEX_ERROR_NOT_SUPPORTED"));	break;
368 					case 3  :	WriteOut(MSG_Get("MSCDEX_ERROR_PATH"));				break;
369 					case 4  :	WriteOut(MSG_Get("MSCDEX_TOO_MANY_DRIVES"));		break;
370 					case 5  :	WriteOut(MSG_Get("MSCDEX_LIMITED_SUPPORT"));		break;
371 					default :	WriteOut(MSG_Get("MSCDEX_UNKNOWN_ERROR"));			break;
372 				};
373 				if (error && error!=5) {
374 					delete newdrive;
375 					return;
376 				}
377 			} else {
378 				/* Give a warning when mount c:\ or the / */
379 #if defined (WIN32) || defined(OS2)
380 				if( (temp_line == "c:\\") || (temp_line == "C:\\") ||
381 				    (temp_line == "c:/") || (temp_line == "C:/")    )
382 					WriteOut(MSG_Get("PROGRAM_MOUNT_WARNING_WIN"));
383 #else
384 				if(temp_line == "/") WriteOut(MSG_Get("PROGRAM_MOUNT_WARNING_OTHER"));
385 #endif
386 				newdrive=new localDrive(temp_line.c_str(),sizes[0],bit8size,sizes[2],sizes[3],mediaid);
387 			}
388 		} else {
389 			WriteOut(MSG_Get("PROGRAM_MOUNT_ILL_TYPE"),type.c_str());
390 			return;
391 		}
392 		if (Drives[drive-'A']) {
393 			WriteOut(MSG_Get("PROGRAM_MOUNT_ALREADY_MOUNTED"),drive,Drives[drive-'A']->GetInfo());
394 			if (newdrive) delete newdrive;
395 			return;
396 		}
397 		if (!newdrive) E_Exit("DOS:Can't create drive");
398 		Drives[drive-'A']=newdrive;
399 		/* Set the correct media byte in the table */
400 		mem_writeb(Real2Phys(dos.tables.mediaid)+(drive-'A')*2,newdrive->GetMediaByte());
401 		WriteOut(MSG_Get("PROGRAM_MOUNT_STATUS_2"),drive,newdrive->GetInfo());
402 		/* check if volume label is given and don't allow it to updated in the future */
403 		if (cmd->FindString("-label",label,true)) newdrive->dirCache.SetLabel(label.c_str(),iscdrom,false);
404 		/* For hard drives set the label to DRIVELETTER_Drive.
405 		 * For floppy drives set the label to DRIVELETTER_Floppy.
406 		 * This way every drive except cdroms should get a label.*/
407 		else if(type == "dir") {
408 			label = drive; label += "_DRIVE";
409 			newdrive->dirCache.SetLabel(label.c_str(),iscdrom,true);
410 		} else if(type == "floppy") {
411 			label = drive; label += "_FLOPPY";
412 			newdrive->dirCache.SetLabel(label.c_str(),iscdrom,true);
413 		}
414 		if(type == "floppy") incrementFDD();
415 		return;
416 showusage:
417 #if defined (WIN32) || defined(OS2)
418 	   WriteOut(MSG_Get("PROGRAM_MOUNT_USAGE"),"d:\\dosprogs","d:\\dosprogs");
419 #else
420 	   WriteOut(MSG_Get("PROGRAM_MOUNT_USAGE"),"~/dosprogs","~/dosprogs");
421 #endif
422 		return;
423 	}
424 };
425 
MOUNT_ProgramStart(Program ** make)426 static void MOUNT_ProgramStart(Program * * make) {
427 	*make=new MOUNT;
428 }
429 
430 class MEM : public Program {
431 public:
Run(void)432 	void Run(void) {
433 		/* Show conventional Memory */
434 		WriteOut("\n");
435 
436 		Bit16u umb_start=dos_infoblock.GetStartOfUMBChain();
437 		Bit8u umb_flag=dos_infoblock.GetUMBChainState();
438 		Bit8u old_memstrat=DOS_GetMemAllocStrategy()&0xff;
439 		if (umb_start!=0xffff) {
440 			if ((umb_flag&1)==1) DOS_LinkUMBsToMemChain(0);
441 			DOS_SetMemAllocStrategy(0);
442 		}
443 
444 		Bit16u seg,blocks;blocks=0xffff;
445 		DOS_AllocateMemory(&seg,&blocks);
446 		if ((machine==MCH_PCJR) && (real_readb(0x2000,0)==0x5a) && (real_readw(0x2000,1)==0) && (real_readw(0x2000,3)==0x7ffe)) {
447 			WriteOut(MSG_Get("PROGRAM_MEM_CONVEN"),0x7ffe*16/1024);
448 		} else WriteOut(MSG_Get("PROGRAM_MEM_CONVEN"),blocks*16/1024);
449 
450 		if (umb_start!=0xffff) {
451 			DOS_LinkUMBsToMemChain(1);
452 			DOS_SetMemAllocStrategy(0x40);	// search in UMBs only
453 
454 			Bit16u largest_block=0,total_blocks=0,block_count=0;
455 			for (;; block_count++) {
456 				blocks=0xffff;
457 				DOS_AllocateMemory(&seg,&blocks);
458 				if (blocks==0) break;
459 				total_blocks+=blocks;
460 				if (blocks>largest_block) largest_block=blocks;
461 				DOS_AllocateMemory(&seg,&blocks);
462 			}
463 
464 			Bit8u current_umb_flag=dos_infoblock.GetUMBChainState();
465 			if ((current_umb_flag&1)!=(umb_flag&1)) DOS_LinkUMBsToMemChain(umb_flag);
466 			DOS_SetMemAllocStrategy(old_memstrat);	// restore strategy
467 
468 			if (block_count>0) WriteOut(MSG_Get("PROGRAM_MEM_UPPER"),total_blocks*16/1024,block_count,largest_block*16/1024);
469 		}
470 
471 		/* Test for and show free XMS */
472 		reg_ax=0x4300;CALLBACK_RunRealInt(0x2f);
473 		if (reg_al==0x80) {
474 			reg_ax=0x4310;CALLBACK_RunRealInt(0x2f);
475 			Bit16u xms_seg=SegValue(es);Bit16u xms_off=reg_bx;
476 			reg_ah=8;
477 			CALLBACK_RunRealFar(xms_seg,xms_off);
478 			if (!reg_bl) {
479 				WriteOut(MSG_Get("PROGRAM_MEM_EXTEND"),reg_dx);
480 			}
481 		}
482 		/* Test for and show free EMS */
483 		Bit16u handle;
484 		char emm[9] = { 'E','M','M','X','X','X','X','0',0 };
485 		if (DOS_OpenFile(emm,0,&handle)) {
486 			DOS_CloseFile(handle);
487 			reg_ah=0x42;
488 			CALLBACK_RunRealInt(0x67);
489 			WriteOut(MSG_Get("PROGRAM_MEM_EXPAND"),reg_bx*16);
490 		}
491 	}
492 };
493 
494 
MEM_ProgramStart(Program ** make)495 static void MEM_ProgramStart(Program * * make) {
496 	*make=new MEM;
497 }
498 
499 extern Bit32u floppytype;
500 
501 
502 class BOOT : public Program {
503 private:
504 
getFSFile_mounted(char const * filename,Bit32u * ksize,Bit32u * bsize,Bit8u * error)505 	FILE *getFSFile_mounted(char const* filename, Bit32u *ksize, Bit32u *bsize, Bit8u *error) {
506 		//if return NULL then put in error the errormessage code if an error was requested
507 		bool tryload = (*error)?true:false;
508 		*error = 0;
509 		Bit8u drive;
510 		FILE *tmpfile;
511 		char fullname[DOS_PATHLENGTH];
512 
513 		localDrive* ldp=0;
514 		if (!DOS_MakeName(const_cast<char*>(filename),fullname,&drive)) return NULL;
515 
516 		try {
517 			ldp=dynamic_cast<localDrive*>(Drives[drive]);
518 			if(!ldp) return NULL;
519 
520 			tmpfile = ldp->GetSystemFilePtr(fullname, "rb");
521 			if(tmpfile == NULL) {
522 				if (!tryload) *error=1;
523 				return NULL;
524 			}
525 
526 			// get file size
527 			fseek(tmpfile,0L, SEEK_END);
528 			*ksize = (ftell(tmpfile) / 1024);
529 			*bsize = ftell(tmpfile);
530 			fclose(tmpfile);
531 
532 			tmpfile = ldp->GetSystemFilePtr(fullname, "rb+");
533 			if(tmpfile == NULL) {
534 //				if (!tryload) *error=2;
535 //				return NULL;
536 				WriteOut(MSG_Get("PROGRAM_BOOT_WRITE_PROTECTED"));
537 				tmpfile = ldp->GetSystemFilePtr(fullname, "rb");
538 				if(tmpfile == NULL) {
539 					if (!tryload) *error=1;
540 					return NULL;
541 				}
542 			}
543 
544 			return tmpfile;
545 		}
546 		catch(...) {
547 			return NULL;
548 		}
549 	}
550 
getFSFile(char const * filename,Bit32u * ksize,Bit32u * bsize,bool tryload=false)551 	FILE *getFSFile(char const * filename, Bit32u *ksize, Bit32u *bsize,bool tryload=false) {
552 		Bit8u error = tryload?1:0;
553 		FILE* tmpfile = getFSFile_mounted(filename,ksize,bsize,&error);
554 		if(tmpfile) return tmpfile;
555 		//File not found on mounted filesystem. Try regular filesystem
556 		std::string filename_s(filename);
557 		Cross::ResolveHomedir(filename_s);
558 		tmpfile = fopen(filename_s.c_str(),"rb+");
559 		if(!tmpfile) {
560 			if( (tmpfile = fopen(filename_s.c_str(),"rb")) ) {
561 				//File exists; So can't be opened in correct mode => error 2
562 //				fclose(tmpfile);
563 //				if(tryload) error = 2;
564 				WriteOut(MSG_Get("PROGRAM_BOOT_WRITE_PROTECTED"));
565 				fseek(tmpfile,0L, SEEK_END);
566 				*ksize = (ftell(tmpfile) / 1024);
567 				*bsize = ftell(tmpfile);
568 				return tmpfile;
569 			}
570 			// Give the delayed errormessages from the mounted variant (or from above)
571 			if(error == 1) WriteOut(MSG_Get("PROGRAM_BOOT_NOT_EXIST"));
572 			if(error == 2) WriteOut(MSG_Get("PROGRAM_BOOT_NOT_OPEN"));
573 			return NULL;
574 		}
575 		fseek(tmpfile,0L, SEEK_END);
576 		*ksize = (ftell(tmpfile) / 1024);
577 		*bsize = ftell(tmpfile);
578 		return tmpfile;
579 	}
580 
printError(void)581 	void printError(void) {
582 		WriteOut(MSG_Get("PROGRAM_BOOT_PRINT_ERROR"));
583 	}
584 
disable_umb_ems_xms(void)585 	void disable_umb_ems_xms(void) {
586 		Section* dos_sec = control->GetSection("dos");
587 		dos_sec->ExecuteDestroy(false);
588 		char test[20];
589 		strcpy(test,"umb=false");
590 		dos_sec->HandleInputline(test);
591 		strcpy(test,"xms=false");
592 		dos_sec->HandleInputline(test);
593 		strcpy(test,"ems=false");
594 		dos_sec->HandleInputline(test);
595 		dos_sec->ExecuteInit(false);
596      }
597 
598 public:
599 
Run(void)600 	void Run(void) {
601 		//Hack To allow long commandlines
602 		ChangeToLongCmd();
603 		/* In secure mode don't allow people to boot stuff.
604 		 * They might try to corrupt the data on it */
605 		if(control->SecureMode()) {
606 			WriteOut(MSG_Get("PROGRAM_CONFIG_SECURE_DISALLOW"));
607 			return;
608 		}
609 
610 		FILE *usefile_1=NULL;
611 		FILE *usefile_2=NULL;
612 		Bitu i=0;
613 		Bit32u floppysize=0;
614 		Bit32u rombytesize_1=0;
615 		Bit32u rombytesize_2=0;
616 		Bit8u drive = 'A';
617 		std::string cart_cmd="";
618 
619 		if(!cmd->GetCount()) {
620 			printError();
621 			return;
622 		}
623 		while(i<cmd->GetCount()) {
624 			if(cmd->FindCommand(i+1, temp_line)) {
625 				if((temp_line == "-l") || (temp_line == "-L")) {
626 					/* Specifying drive... next argument then is the drive */
627 					i++;
628 					if(cmd->FindCommand(i+1, temp_line)) {
629 						drive=toupper(temp_line[0]);
630 						if ((drive != 'A') && (drive != 'C') && (drive != 'D')) {
631 							printError();
632 							return;
633 						}
634 
635 					} else {
636 						printError();
637 						return;
638 					}
639 					i++;
640 					continue;
641 				}
642 
643 				if((temp_line == "-e") || (temp_line == "-E")) {
644 					/* Command mode for PCJr cartridges */
645 					i++;
646 					if(cmd->FindCommand(i + 1, temp_line)) {
647 						for(size_t ct = 0;ct < temp_line.size();ct++) temp_line[ct] = toupper(temp_line[ct]);
648 						cart_cmd = temp_line;
649 					} else {
650 						printError();
651 						return;
652 					}
653 					i++;
654 					continue;
655 				}
656 
657 				WriteOut(MSG_Get("PROGRAM_BOOT_IMAGE_OPEN"), temp_line.c_str());
658 				Bit32u rombytesize;
659 				FILE *usefile = getFSFile(temp_line.c_str(), &floppysize, &rombytesize);
660 				if(usefile != NULL) {
661 					if(diskSwap[i] != NULL) delete diskSwap[i];
662 					diskSwap[i] = new imageDisk(usefile, (Bit8u *)temp_line.c_str(), floppysize, false);
663 					if (usefile_1==NULL) {
664 						usefile_1=usefile;
665 						rombytesize_1=rombytesize;
666 					} else {
667 						usefile_2=usefile;
668 						rombytesize_2=rombytesize;
669 					}
670 				} else {
671 					WriteOut(MSG_Get("PROGRAM_BOOT_IMAGE_NOT_OPEN"), temp_line.c_str());
672 					return;
673 				}
674 
675 			}
676 			i++;
677 		}
678 
679 		swapPosition = 0;
680 
681 		swapInDisks();
682 
683 		if(imageDiskList[drive-65]==NULL) {
684 			WriteOut(MSG_Get("PROGRAM_BOOT_UNABLE"), drive);
685 			return;
686 		}
687 
688 		bootSector bootarea;
689 		imageDiskList[drive-65]->Read_Sector(0,0,1,(Bit8u *)&bootarea);
690 		if ((bootarea.rawdata[0]==0x50) && (bootarea.rawdata[1]==0x43) && (bootarea.rawdata[2]==0x6a) && (bootarea.rawdata[3]==0x72)) {
691 			if (machine!=MCH_PCJR) WriteOut(MSG_Get("PROGRAM_BOOT_CART_WO_PCJR"));
692 			else {
693 				Bit8u rombuf[65536];
694 				Bits cfound_at=-1;
695 				if (cart_cmd!="") {
696 					/* read cartridge data into buffer */
697 					fseek(usefile_1,0x200L, SEEK_SET);
698 					fread(rombuf, 1, rombytesize_1-0x200, usefile_1);
699 
700 					char cmdlist[1024];
701 					cmdlist[0]=0;
702 					Bitu ct=6;
703 					Bits clen=rombuf[ct];
704 					char buf[257];
705 					if (cart_cmd=="?") {
706 						while (clen!=0) {
707 							strncpy(buf,(char*)&rombuf[ct+1],clen);
708 							buf[clen]=0;
709 							upcase(buf);
710 							strcat(cmdlist," ");
711 							strcat(cmdlist,buf);
712 							ct+=1+clen+3;
713 							if (ct>sizeof(cmdlist)) break;
714 							clen=rombuf[ct];
715 						}
716 						if (ct>6) {
717 							WriteOut(MSG_Get("PROGRAM_BOOT_CART_LIST_CMDS"),cmdlist);
718 						} else {
719 							WriteOut(MSG_Get("PROGRAM_BOOT_CART_NO_CMDS"));
720 						}
721 						for(Bitu dct=0;dct<MAX_SWAPPABLE_DISKS;dct++) {
722 							if(diskSwap[dct]!=NULL) {
723 								delete diskSwap[dct];
724 								diskSwap[dct]=NULL;
725 							}
726 						}
727 						//fclose(usefile_1); //delete diskSwap closes the file
728 						return;
729 					} else {
730 						while (clen!=0) {
731 							strncpy(buf,(char*)&rombuf[ct+1],clen);
732 							buf[clen]=0;
733 							upcase(buf);
734 							strcat(cmdlist," ");
735 							strcat(cmdlist,buf);
736 							ct+=1+clen;
737 
738 							if (cart_cmd==buf) {
739 								cfound_at=ct;
740 								break;
741 							}
742 
743 							ct+=3;
744 							if (ct>sizeof(cmdlist)) break;
745 							clen=rombuf[ct];
746 						}
747 						if (cfound_at<=0) {
748 							if (ct>6) {
749 								WriteOut(MSG_Get("PROGRAM_BOOT_CART_LIST_CMDS"),cmdlist);
750 							} else {
751 								WriteOut(MSG_Get("PROGRAM_BOOT_CART_NO_CMDS"));
752 							}
753 							for(Bitu dct=0;dct<MAX_SWAPPABLE_DISKS;dct++) {
754 								if(diskSwap[dct]!=NULL) {
755 									delete diskSwap[dct];
756 									diskSwap[dct]=NULL;
757 								}
758 							}
759 							//fclose(usefile_1); //Delete diskSwap closes the file
760 							return;
761 						}
762 					}
763 				}
764 
765 				disable_umb_ems_xms();
766 				void PreparePCJRCartRom(void);
767 				PreparePCJRCartRom();
768 
769 				if (usefile_1==NULL) return;
770 
771 				Bit32u sz1,sz2;
772 				FILE *tfile = getFSFile("system.rom", &sz1, &sz2, true);
773 				if (tfile!=NULL) {
774 					fseek(tfile, 0x3000L, SEEK_SET);
775 					Bit32u drd=(Bit32u)fread(rombuf, 1, 0xb000, tfile);
776 					if (drd==0xb000) {
777 						for(i=0;i<0xb000;i++) phys_writeb(0xf3000+i,rombuf[i]);
778 					}
779 					fclose(tfile);
780 				}
781 
782 				if (usefile_2!=NULL) {
783 					fseek(usefile_2, 0x0L, SEEK_SET);
784 					fread(rombuf, 1, 0x200, usefile_2);
785 					PhysPt romseg_pt=host_readw(&rombuf[0x1ce])<<4;
786 
787 					/* read cartridge data into buffer */
788 					fseek(usefile_2, 0x200L, SEEK_SET);
789 					fread(rombuf, 1, rombytesize_2-0x200, usefile_2);
790 					//fclose(usefile_2); //usefile_2 is in diskSwap structure which should be deleted to close the file
791 
792 					/* write cartridge data into ROM */
793 					for(i=0;i<rombytesize_2-0x200;i++) phys_writeb(romseg_pt+i,rombuf[i]);
794 				}
795 
796 				fseek(usefile_1, 0x0L, SEEK_SET);
797 				fread(rombuf, 1, 0x200, usefile_1);
798 				Bit16u romseg=host_readw(&rombuf[0x1ce]);
799 
800 				/* read cartridge data into buffer */
801 				fseek(usefile_1,0x200L, SEEK_SET);
802 				fread(rombuf, 1, rombytesize_1-0x200, usefile_1);
803 				//fclose(usefile_1); //usefile_1 is in diskSwap structure which should be deleted to close the file
804 
805 				/* write cartridge data into ROM */
806 				for(i=0;i<rombytesize_1-0x200;i++) phys_writeb((romseg<<4)+i,rombuf[i]);
807 
808 				//Close cardridges
809 				for(Bitu dct=0;dct<MAX_SWAPPABLE_DISKS;dct++) {
810 					if(diskSwap[dct]!=NULL) {
811 						delete diskSwap[dct];
812 						diskSwap[dct]=NULL;
813 					}
814 				}
815 
816 
817 				if (cart_cmd=="") {
818 					Bit32u old_int18=mem_readd(0x60);
819 					/* run cartridge setup */
820 					SegSet16(ds,romseg);
821 					SegSet16(es,romseg);
822 					SegSet16(ss,0x8000);
823 					reg_esp=0xfffe;
824 					CALLBACK_RunRealFar(romseg,0x0003);
825 
826 					Bit32u new_int18=mem_readd(0x60);
827 					if (old_int18!=new_int18) {
828 						/* boot cartridge (int18) */
829 						SegSet16(cs,RealSeg(new_int18));
830 						reg_ip = RealOff(new_int18);
831 					}
832 				} else {
833 					if (cfound_at>0) {
834 						/* run cartridge setup */
835 						SegSet16(ds,dos.psp());
836 						SegSet16(es,dos.psp());
837 						CALLBACK_RunRealFar(romseg,cfound_at);
838 					}
839 				}
840 			}
841 		} else {
842 			disable_umb_ems_xms();
843 			void RemoveEMSPageFrame(void);
844 			RemoveEMSPageFrame();
845 			WriteOut(MSG_Get("PROGRAM_BOOT_BOOT"), drive);
846 			for(i=0;i<512;i++) real_writeb(0, 0x7c00 + i, bootarea.rawdata[i]);
847 
848 			/* create appearance of floppy drive DMA usage (Demon's Forge) */
849 			if (!IS_TANDY_ARCH && floppysize!=0) GetDMAChannel(2)->tcount=true;
850 
851 			/* revector some dos-allocated interrupts */
852 			real_writed(0,0x01*4,0xf000ff53);
853 			real_writed(0,0x03*4,0xf000ff53);
854 
855 			SegSet16(cs, 0);
856 			reg_ip = 0x7c00;
857 			SegSet16(ds, 0);
858 			SegSet16(es, 0);
859 			/* set up stack at a safe place */
860 			SegSet16(ss, 0x7000);
861 			reg_esp = 0x100;
862 			reg_esi = 0;
863 			reg_ecx = 1;
864 			reg_ebp = 0;
865 			reg_eax = 0;
866 			reg_edx = 0; //Head 0 drive 0
867 			reg_ebx= 0x7c00; //Real code probably uses bx to load the image
868 		}
869 	}
870 };
871 
BOOT_ProgramStart(Program ** make)872 static void BOOT_ProgramStart(Program * * make) {
873 	*make=new BOOT;
874 }
875 
876 
877 class LOADROM : public Program {
878 public:
Run(void)879 	void Run(void) {
880 		if (!(cmd->FindCommand(1, temp_line))) {
881 			WriteOut(MSG_Get("PROGRAM_LOADROM_SPECIFY_FILE"));
882 			return;
883 		}
884 
885 		Bit8u drive;
886 		char fullname[DOS_PATHLENGTH];
887 		localDrive* ldp=0;
888 		if (!DOS_MakeName((char *)temp_line.c_str(),fullname,&drive)) return;
889 
890 		try {
891 			/* try to read ROM file into buffer */
892 			ldp=dynamic_cast<localDrive*>(Drives[drive]);
893 			if(!ldp) return;
894 
895 			FILE *tmpfile = ldp->GetSystemFilePtr(fullname, "rb");
896 			if(tmpfile == NULL) {
897 				WriteOut(MSG_Get("PROGRAM_LOADROM_CANT_OPEN"));
898 				return;
899 			}
900 			fseek(tmpfile, 0L, SEEK_END);
901 			if (ftell(tmpfile)>0x8000) {
902 				WriteOut(MSG_Get("PROGRAM_LOADROM_TOO_LARGE"));
903 				fclose(tmpfile);
904 				return;
905 			}
906 			fseek(tmpfile, 0L, SEEK_SET);
907 			Bit8u rom_buffer[0x8000];
908 			Bitu data_read = fread(rom_buffer, 1, 0x8000, tmpfile);
909 			fclose(tmpfile);
910 
911 			/* try to identify ROM type */
912 			PhysPt rom_base = 0;
913 			if (data_read >= 0x4000 && rom_buffer[0] == 0x55 && rom_buffer[1] == 0xaa &&
914 				rom_buffer[3] == 0xeb && strncmp((char*)(&rom_buffer[0x1e]), "IBM", 3) == 0) {
915 
916 				if (!IS_EGAVGA_ARCH) {
917 					WriteOut(MSG_Get("PROGRAM_LOADROM_INCOMPATIBLE"));
918 					return;
919 				}
920 				rom_base = PhysMake(0xc000, 0); // video BIOS
921 			}
922 			else if (data_read == 0x8000 && rom_buffer[0] == 0xe9 && rom_buffer[1] == 0x8f &&
923 				rom_buffer[2] == 0x7e && strncmp((char*)(&rom_buffer[0x4cd4]), "IBM", 3) == 0) {
924 
925 				rom_base = PhysMake(0xf600, 0); // BASIC
926 			}
927 
928 			if (rom_base) {
929 				/* write buffer into ROM */
930 				for (Bitu i=0; i<data_read; i++) phys_writeb(rom_base + i, rom_buffer[i]);
931 
932 				if (rom_base == 0xc0000) {
933 					/* initialize video BIOS */
934 					phys_writeb(PhysMake(0xf000, 0xf065), 0xcf);
935 					reg_flags &= ~FLAG_IF;
936 					CALLBACK_RunRealFar(0xc000, 0x0003);
937 					LOG_MSG("Video BIOS ROM loaded and initialized.");
938 				}
939 				else WriteOut(MSG_Get("PROGRAM_LOADROM_BASIC_LOADED"));
940 			}
941 			else WriteOut(MSG_Get("PROGRAM_LOADROM_UNRECOGNIZED"));
942 		}
943 		catch(...) {
944 			return;
945 		}
946 	}
947 };
948 
LOADROM_ProgramStart(Program ** make)949 static void LOADROM_ProgramStart(Program * * make) {
950 	*make=new LOADROM;
951 }
952 
953 
954 // LOADFIX
955 
956 class LOADFIX : public Program {
957 public:
958 	void Run(void);
959 };
960 
Run(void)961 void LOADFIX::Run(void)
962 {
963 	Bit16u commandNr	= 1;
964 	Bit16u kb			= 64;
965 	if (cmd->FindCommand(commandNr,temp_line)) {
966 		if (temp_line[0]=='-') {
967 			char ch = temp_line[1];
968 			if ((*upcase(&ch)=='D') || (*upcase(&ch)=='F')) {
969 				// Deallocate all
970 				DOS_FreeProcessMemory(0x40);
971 				WriteOut(MSG_Get("PROGRAM_LOADFIX_DEALLOCALL"),kb);
972 				return;
973 			} else {
974 				// Set mem amount to allocate
975 				kb = atoi(temp_line.c_str()+1);
976 				if (kb==0) kb=64;
977 				commandNr++;
978 			}
979 		}
980 	}
981 	// Allocate Memory
982 	Bit16u segment;
983 	Bit16u blocks = kb*1024/16;
984 	if (DOS_AllocateMemory(&segment,&blocks)) {
985 		DOS_MCB mcb((Bit16u)(segment-1));
986 		mcb.SetPSPSeg(0x40);			// use fake segment
987 		WriteOut(MSG_Get("PROGRAM_LOADFIX_ALLOC"),kb);
988 		// Prepare commandline...
989 		if (cmd->FindCommand(commandNr++,temp_line)) {
990 			// get Filename
991 			char filename[128];
992 			safe_strncpy(filename,temp_line.c_str(),128);
993 			// Setup commandline
994 			bool ok;
995 			char args[256];
996 			args[0] = 0;
997 			do {
998 				ok = cmd->FindCommand(commandNr++,temp_line);
999 				if(sizeof(args)-strlen(args)-1 < temp_line.length()+1)
1000 					break;
1001 				strcat(args,temp_line.c_str());
1002 				strcat(args," ");
1003 			} while (ok);
1004 			// Use shell to start program
1005 			DOS_Shell shell;
1006 			shell.Execute(filename,args);
1007 			DOS_FreeMemory(segment);
1008 			WriteOut(MSG_Get("PROGRAM_LOADFIX_DEALLOC"),kb);
1009 		}
1010 	} else {
1011 		WriteOut(MSG_Get("PROGRAM_LOADFIX_ERROR"),kb);
1012 	}
1013 }
1014 
LOADFIX_ProgramStart(Program ** make)1015 static void LOADFIX_ProgramStart(Program * * make) {
1016 	*make=new LOADFIX;
1017 }
1018 
1019 // RESCAN
1020 
1021 class RESCAN : public Program {
1022 public:
1023 	void Run(void);
1024 };
1025 
Run(void)1026 void RESCAN::Run(void)
1027 {
1028 	bool all = false;
1029 
1030 	Bit8u drive = DOS_GetDefaultDrive();
1031 
1032 	if(cmd->FindCommand(1,temp_line)) {
1033 		//-A -All /A /All
1034 		if(temp_line.size() >= 2 && (temp_line[0] == '-' ||temp_line[0] =='/')&& (temp_line[1] == 'a' || temp_line[1] =='A') ) all = true;
1035 		else if(temp_line.size() == 2 && temp_line[1] == ':') {
1036 			lowcase(temp_line);
1037 			drive  = temp_line[0] - 'a';
1038 		}
1039 	}
1040 	// Get current drive
1041 	if (all) {
1042 		for(Bitu i =0; i<DOS_DRIVES;i++) {
1043 			if (Drives[i]) Drives[i]->EmptyCache();
1044 		}
1045 		WriteOut(MSG_Get("PROGRAM_RESCAN_SUCCESS"));
1046 	} else {
1047 		if (drive < DOS_DRIVES && Drives[drive]) {
1048 			Drives[drive]->EmptyCache();
1049 			WriteOut(MSG_Get("PROGRAM_RESCAN_SUCCESS"));
1050 		}
1051 	}
1052 }
1053 
RESCAN_ProgramStart(Program ** make)1054 static void RESCAN_ProgramStart(Program * * make) {
1055 	*make=new RESCAN;
1056 }
1057 
1058 class INTRO : public Program {
1059 public:
DisplayMount(void)1060 	void DisplayMount(void) {
1061 		/* Basic mounting has a version for each operating system.
1062 		 * This is done this way so both messages appear in the language file*/
1063 		WriteOut(MSG_Get("PROGRAM_INTRO_MOUNT_START"));
1064 #if (WIN32)
1065 		WriteOut(MSG_Get("PROGRAM_INTRO_MOUNT_WINDOWS"));
1066 #else
1067 		WriteOut(MSG_Get("PROGRAM_INTRO_MOUNT_OTHER"));
1068 #endif
1069 		WriteOut(MSG_Get("PROGRAM_INTRO_MOUNT_END"));
1070 	}
1071 
Run(void)1072 	void Run(void) {
1073 		/* Only run if called from the first shell (Xcom TFTD runs any intro file in the path) */
1074 		if(DOS_PSP(dos.psp()).GetParent() != DOS_PSP(DOS_PSP(dos.psp()).GetParent()).GetParent()) return;
1075 		if(cmd->FindExist("cdrom",false)) {
1076 			WriteOut(MSG_Get("PROGRAM_INTRO_CDROM"));
1077 			return;
1078 		}
1079 		if(cmd->FindExist("mount",false)) {
1080 			WriteOut("\033[2J");//Clear screen before printing
1081 			DisplayMount();
1082 			return;
1083 		}
1084 		if(cmd->FindExist("special",false)) {
1085 			WriteOut(MSG_Get("PROGRAM_INTRO_SPECIAL"));
1086 			return;
1087 		}
1088 		/* Default action is to show all pages */
1089 		WriteOut(MSG_Get("PROGRAM_INTRO"));
1090 		Bit8u c;Bit16u n=1;
1091 		DOS_ReadFile (STDIN,&c,&n);
1092 		DisplayMount();
1093 		DOS_ReadFile (STDIN,&c,&n);
1094 		WriteOut(MSG_Get("PROGRAM_INTRO_CDROM"));
1095 		DOS_ReadFile (STDIN,&c,&n);
1096 		WriteOut(MSG_Get("PROGRAM_INTRO_SPECIAL"));
1097 	}
1098 };
1099 
INTRO_ProgramStart(Program ** make)1100 static void INTRO_ProgramStart(Program * * make) {
1101 	*make=new INTRO;
1102 }
1103 
1104 class IMGMOUNT : public Program {
1105 public:
Run(void)1106 	void Run(void) {
1107 		//Hack To allow long commandlines
1108 		ChangeToLongCmd();
1109 		/* In secure mode don't allow people to change imgmount points.
1110 		 * Neither mount nor unmount */
1111 		if(control->SecureMode()) {
1112 			WriteOut(MSG_Get("PROGRAM_CONFIG_SECURE_DISALLOW"));
1113 			return;
1114 		}
1115 		DOS_Drive * newdrive = NULL;
1116 		imageDisk * newImage = NULL;
1117 		Bit32u imagesize;
1118 		char drive;
1119 		std::string label;
1120 		std::vector<std::string> paths;
1121 		std::string umount;
1122 		/* Check for unmounting */
1123 		if (cmd->FindString("-u",umount,false)) {
1124 			umount[0] = toupper(umount[0]);
1125 			int i_drive = umount[0]-'A';
1126 				if (i_drive < DOS_DRIVES && i_drive >= 0 && Drives[i_drive]) {
1127 					switch (DriveManager::UnmountDrive(i_drive)) {
1128 					case 0:
1129 						Drives[i_drive] = 0;
1130 						if (i_drive == DOS_GetDefaultDrive())
1131 							DOS_SetDrive(toupper('Z') - 'A');
1132 						WriteOut(MSG_Get("PROGRAM_MOUNT_UMOUNT_SUCCESS"),umount[0]);
1133 						break;
1134 					case 1:
1135 						WriteOut(MSG_Get("PROGRAM_MOUNT_UMOUNT_NO_VIRTUAL"));
1136 						break;
1137 					case 2:
1138 						WriteOut(MSG_Get("MSCDEX_ERROR_MULTIPLE_CDROMS"));
1139 						break;
1140 					}
1141 				} else {
1142 					WriteOut(MSG_Get("PROGRAM_MOUNT_UMOUNT_NOT_MOUNTED"),umount[0]);
1143 				}
1144 			return;
1145 		}
1146 
1147 
1148 		std::string type="hdd";
1149 		std::string fstype="fat";
1150 		cmd->FindString("-t",type,true);
1151 		cmd->FindString("-fs",fstype,true);
1152 		if(type == "cdrom") type = "iso"; //Tiny hack for people who like to type -t cdrom
1153 		Bit8u mediaid;
1154 		if (type=="floppy" || type=="hdd" || type=="iso") {
1155 			Bit16u sizes[4];
1156 			bool imgsizedetect=false;
1157 
1158 			std::string str_size;
1159 			mediaid=0xF8;
1160 
1161 			if (type=="floppy") {
1162 				mediaid=0xF0;
1163 			} else if (type=="iso") {
1164 				//str_size="2048,1,65535,0";	// ignored, see drive_iso.cpp (AllocationInfo)
1165 				mediaid=0xF8;
1166 				fstype = "iso";
1167 			}
1168 			cmd->FindString("-size",str_size,true);
1169 			if ((type=="hdd") && (str_size.size()==0)) {
1170 				imgsizedetect=true;
1171 			} else {
1172 				char number[20];
1173 				const char * scan=str_size.c_str();
1174 				Bitu index=0;Bitu count=0;
1175 
1176 				while (*scan) {
1177 					if (*scan==',') {
1178 						number[index]=0;sizes[count++]=atoi(number);
1179 						index=0;
1180 					} else number[index++]=*scan;
1181 					scan++;
1182 				}
1183 				number[index]=0;sizes[count++]=atoi(number);
1184 			}
1185 
1186 			if(fstype=="fat" || fstype=="iso") {
1187 				// get the drive letter
1188 				if (!cmd->FindCommand(1,temp_line) || (temp_line.size() > 2) || ((temp_line.size()>1) && (temp_line[1]!=':'))) {
1189 					WriteOut_NoParsing(MSG_Get("PROGRAM_IMGMOUNT_SPECIFY_DRIVE"));
1190 					return;
1191 				}
1192 				drive=toupper(temp_line[0]);
1193 				if (!isalpha(drive)) {
1194 					WriteOut_NoParsing(MSG_Get("PROGRAM_IMGMOUNT_SPECIFY_DRIVE"));
1195 					return;
1196 				}
1197 			} else if (fstype=="none") {
1198 				cmd->FindCommand(1,temp_line);
1199 				if ((temp_line.size() > 1) || (!isdigit(temp_line[0]))) {
1200 					WriteOut_NoParsing(MSG_Get("PROGRAM_IMGMOUNT_SPECIFY2"));
1201 					return;
1202 				}
1203 				drive=temp_line[0];
1204 				if ((drive<'0') || (drive>3+'0')) {
1205 					WriteOut_NoParsing(MSG_Get("PROGRAM_IMGMOUNT_SPECIFY2"));
1206 					return;
1207 				}
1208 			} else {
1209 				WriteOut(MSG_Get("PROGRAM_IMGMOUNT_FORMAT_UNSUPPORTED"),fstype.c_str());
1210 				return;
1211 			}
1212 
1213 			// find all file parameters, assuming that all option parameters have been removed
1214 			while(cmd->FindCommand((unsigned int)(paths.size() + 2), temp_line) && temp_line.size()) {
1215 
1216 				struct stat test;
1217 				if (stat(temp_line.c_str(),&test)) {
1218 					//See if it works if the ~ are written out
1219 					std::string homedir(temp_line);
1220 					Cross::ResolveHomedir(homedir);
1221 					if(!stat(homedir.c_str(),&test)) {
1222 						temp_line = homedir;
1223 					} else {
1224 						// convert dosbox filename to system filename
1225 						char fullname[CROSS_LEN];
1226 						char tmp[CROSS_LEN];
1227 						safe_strncpy(tmp, temp_line.c_str(), CROSS_LEN);
1228 
1229 						Bit8u dummy;
1230 						if (!DOS_MakeName(tmp, fullname, &dummy) || strncmp(Drives[dummy]->GetInfo(),"local directory",15)) {
1231 							WriteOut(MSG_Get("PROGRAM_IMGMOUNT_NON_LOCAL_DRIVE"));
1232 							return;
1233 						}
1234 
1235 						localDrive *ldp = dynamic_cast<localDrive*>(Drives[dummy]);
1236 						if (ldp==NULL) {
1237 							WriteOut(MSG_Get("PROGRAM_IMGMOUNT_FILE_NOT_FOUND"));
1238 							return;
1239 						}
1240 						ldp->GetSystemFilename(tmp, fullname);
1241 						temp_line = tmp;
1242 
1243 						if (stat(temp_line.c_str(),&test)) {
1244 							WriteOut(MSG_Get("PROGRAM_IMGMOUNT_FILE_NOT_FOUND"));
1245 							return;
1246 						}
1247 					}
1248 				}
1249 				if (S_ISDIR(test.st_mode)) {
1250 					WriteOut(MSG_Get("PROGRAM_IMGMOUNT_MOUNT"));
1251 					return;
1252 				}
1253 				paths.push_back(temp_line);
1254 			}
1255 			if (paths.size() == 0) {
1256 				WriteOut(MSG_Get("PROGRAM_IMGMOUNT_SPECIFY_FILE"));
1257 				return;
1258 			}
1259 			if (paths.size() == 1)
1260 				temp_line = paths[0];
1261 
1262 			if(fstype=="fat") {
1263 				if (imgsizedetect) {
1264 					FILE * diskfile = fopen(temp_line.c_str(), "rb+");
1265 					if (!diskfile) {
1266 						WriteOut(MSG_Get("PROGRAM_IMGMOUNT_INVALID_IMAGE"));
1267 						return;
1268 					}
1269 					fseek(diskfile, 0L, SEEK_END);
1270 					Bit32u fcsize = (Bit32u)(ftell(diskfile) / 512L);
1271 					Bit8u buf[512];
1272 					fseek(diskfile, 0L, SEEK_SET);
1273 					if (fread(buf,sizeof(Bit8u),512,diskfile)<512) {
1274 						fclose(diskfile);
1275 						WriteOut(MSG_Get("PROGRAM_IMGMOUNT_INVALID_IMAGE"));
1276 						return;
1277 					}
1278 					fclose(diskfile);
1279 					if ((buf[510]!=0x55) || (buf[511]!=0xaa)) {
1280 						WriteOut(MSG_Get("PROGRAM_IMGMOUNT_INVALID_GEOMETRY"));
1281 						return;
1282 					}
1283 					Bitu sectors=(Bitu)(fcsize/(16*63));
1284 					if (sectors*16*63!=fcsize) {
1285 						WriteOut(MSG_Get("PROGRAM_IMGMOUNT_INVALID_GEOMETRY"));
1286 						return;
1287 					}
1288 					sizes[0]=512;	sizes[1]=63;	sizes[2]=16;	sizes[3]=sectors;
1289 					LOG_MSG("autosized image file: %d:%d:%d:%d",sizes[0],sizes[1],sizes[2],sizes[3]);
1290 				}
1291 
1292 				if (Drives[drive-'A']) {
1293 					WriteOut(MSG_Get("PROGRAM_IMGMOUNT_ALREADY_MOUNTED"));
1294 					return;
1295 				}
1296 
1297 				std::vector<DOS_Drive*> imgDisks;
1298 				std::vector<std::string>::size_type i;
1299 				std::vector<DOS_Drive*>::size_type ct;
1300 
1301 				for (i = 0; i < paths.size(); i++) {
1302 					DOS_Drive* newDrive = new fatDrive(paths[i].c_str(),sizes[0],sizes[1],sizes[2],sizes[3],0);
1303 					imgDisks.push_back(newDrive);
1304 					if(!(dynamic_cast<fatDrive*>(newDrive))->created_successfully) {
1305 						WriteOut(MSG_Get("PROGRAM_IMGMOUNT_CANT_CREATE"));
1306 						for(ct = 0; ct < imgDisks.size(); ct++) {
1307 							delete imgDisks[ct];
1308 						}
1309 						return;
1310 					}
1311 				}
1312 
1313 				// Update DriveManager
1314 				for(ct = 0; ct < imgDisks.size(); ct++) {
1315 					DriveManager::AppendDisk(drive - 'A', imgDisks[ct]);
1316 				}
1317 				DriveManager::InitializeDrive(drive - 'A');
1318 
1319 				// Set the correct media byte in the table
1320 				mem_writeb(Real2Phys(dos.tables.mediaid) + (drive - 'A') * 2, mediaid);
1321 
1322 				/* Command uses dta so set it to our internal dta */
1323 				RealPt save_dta = dos.dta();
1324 				dos.dta(dos.tables.tempdta);
1325 
1326 				for(ct = 0; ct < imgDisks.size(); ct++) {
1327 					DriveManager::CycleAllDisks();
1328 
1329 					char root[7] = {drive,':','\\','*','.','*',0};
1330 					DOS_FindFirst(root, DOS_ATTR_VOLUME); // force obtaining the label and saving it in dirCache
1331 				}
1332 				dos.dta(save_dta);
1333 
1334 				std::string tmp(paths[0]);
1335 				for (i = 1; i < paths.size(); i++) {
1336 					tmp += "; " + paths[i];
1337 				}
1338 				WriteOut(MSG_Get("PROGRAM_MOUNT_STATUS_2"), drive, tmp.c_str());
1339 
1340 				if (paths.size() == 1) {
1341 					newdrive = imgDisks[0];
1342 					if(((fatDrive *)newdrive)->loadedDisk->hardDrive) {
1343 						if(imageDiskList[2] == NULL) {
1344 							imageDiskList[2] = ((fatDrive *)newdrive)->loadedDisk;
1345 							updateDPT();
1346 							return;
1347 						}
1348 						if(imageDiskList[3] == NULL) {
1349 							imageDiskList[3] = ((fatDrive *)newdrive)->loadedDisk;
1350 							updateDPT();
1351 							return;
1352 						}
1353 					}
1354 					if(!((fatDrive *)newdrive)->loadedDisk->hardDrive) {
1355 						imageDiskList[0] = ((fatDrive *)newdrive)->loadedDisk;
1356 					}
1357 				}
1358 			} else if (fstype=="iso") {
1359 
1360 				if (Drives[drive-'A']) {
1361 					WriteOut(MSG_Get("PROGRAM_IMGMOUNT_ALREADY_MOUNTED"));
1362 					return;
1363 				}
1364 				MSCDEX_SetCDInterface(CDROM_USE_SDL, -1);
1365 				// create new drives for all images
1366 				std::vector<DOS_Drive*> isoDisks;
1367 				std::vector<std::string>::size_type i;
1368 				std::vector<DOS_Drive*>::size_type ct;
1369 				for (i = 0; i < paths.size(); i++) {
1370 					int error = -1;
1371 					DOS_Drive* newDrive = new isoDrive(drive, paths[i].c_str(), mediaid, error);
1372 					isoDisks.push_back(newDrive);
1373 					switch (error) {
1374 						case 0  :	break;
1375 						case 1  :	WriteOut(MSG_Get("MSCDEX_ERROR_MULTIPLE_CDROMS"));	break;
1376 						case 2  :	WriteOut(MSG_Get("MSCDEX_ERROR_NOT_SUPPORTED"));	break;
1377 						case 3  :	WriteOut(MSG_Get("MSCDEX_ERROR_OPEN"));				break;
1378 						case 4  :	WriteOut(MSG_Get("MSCDEX_TOO_MANY_DRIVES"));		break;
1379 						case 5  :	WriteOut(MSG_Get("MSCDEX_LIMITED_SUPPORT"));		break;
1380 						case 6  :	WriteOut(MSG_Get("MSCDEX_INVALID_FILEFORMAT"));		break;
1381 						default :	WriteOut(MSG_Get("MSCDEX_UNKNOWN_ERROR"));			break;
1382 					}
1383 					// error: clean up and leave
1384 					if (error) {
1385 						for(ct = 0; ct < isoDisks.size(); ct++) {
1386 							delete isoDisks[ct];
1387 						}
1388 						return;
1389 					}
1390 				}
1391 				// Update DriveManager
1392 				for(ct = 0; ct < isoDisks.size(); ct++) {
1393 					DriveManager::AppendDisk(drive - 'A', isoDisks[ct]);
1394 				}
1395 				DriveManager::InitializeDrive(drive - 'A');
1396 
1397 				// Set the correct media byte in the table
1398 				mem_writeb(Real2Phys(dos.tables.mediaid) + (drive - 'A') * 2, mediaid);
1399 
1400 				// Print status message (success)
1401 				WriteOut(MSG_Get("MSCDEX_SUCCESS"));
1402 				std::string tmp(paths[0]);
1403 				for (i = 1; i < paths.size(); i++) {
1404 					tmp += "; " + paths[i];
1405 				}
1406 				WriteOut(MSG_Get("PROGRAM_MOUNT_STATUS_2"), drive, tmp.c_str());
1407 
1408 			} else {
1409 				FILE *newDisk = fopen(temp_line.c_str(), "rb+");
1410 				if (!newDisk) {
1411 					WriteOut(MSG_Get("PROGRAM_IMGMOUNT_INVALID_IMAGE"));
1412 					return;
1413 				}
1414 				fseek(newDisk,0L, SEEK_END);
1415 				imagesize = (ftell(newDisk) / 1024);
1416 
1417 				newImage = new imageDisk(newDisk, (Bit8u *)temp_line.c_str(), imagesize, (imagesize > 2880));
1418 				if(imagesize>2880) newImage->Set_Geometry(sizes[2],sizes[3],sizes[1],sizes[0]);
1419 			}
1420 		} else {
1421 			WriteOut(MSG_Get("PROGRAM_IMGMOUNT_TYPE_UNSUPPORTED"),type.c_str());
1422 			return;
1423 		}
1424 
1425 		if (fstype=="none") {
1426 			if(imageDiskList[drive-'0'] != NULL) delete imageDiskList[drive-'0'];
1427 			imageDiskList[drive-'0'] = newImage;
1428 			updateDPT();
1429 			WriteOut(MSG_Get("PROGRAM_IMGMOUNT_MOUNT_NUMBER"),drive-'0',temp_line.c_str());
1430 		}
1431 
1432 		// check if volume label is given. becareful for cdrom
1433 		//if (cmd->FindString("-label",label,true)) newdrive->dirCache.SetLabel(label.c_str());
1434 		return;
1435 	}
1436 };
1437 
IMGMOUNT_ProgramStart(Program ** make)1438 void IMGMOUNT_ProgramStart(Program * * make) {
1439 	*make=new IMGMOUNT;
1440 }
1441 
1442 
1443 Bitu DOS_SwitchKeyboardLayout(const char* new_layout, Bit32s& tried_cp);
1444 Bitu DOS_LoadKeyboardLayout(const char * layoutname, Bit32s codepage, const char * codepagefile);
1445 const char* DOS_GetLoadedLayout(void);
1446 
1447 class KEYB : public Program {
1448 public:
1449 	void Run(void);
1450 };
1451 
Run(void)1452 void KEYB::Run(void) {
1453 	if (cmd->FindCommand(1,temp_line)) {
1454 		if (cmd->FindString("?",temp_line,false)) {
1455 			WriteOut(MSG_Get("PROGRAM_KEYB_SHOWHELP"));
1456 		} else {
1457 			/* first parameter is layout ID */
1458 			Bitu keyb_error=0;
1459 			std::string cp_string;
1460 			Bit32s tried_cp = -1;
1461 			if (cmd->FindCommand(2,cp_string)) {
1462 				/* second parameter is codepage number */
1463 				tried_cp=atoi(cp_string.c_str());
1464 				char cp_file_name[256];
1465 				if (cmd->FindCommand(3,cp_string)) {
1466 					/* third parameter is codepage file */
1467 					strcpy(cp_file_name, cp_string.c_str());
1468 				} else {
1469 					/* no codepage file specified, use automatic selection */
1470 					strcpy(cp_file_name, "auto");
1471 				}
1472 
1473 				keyb_error=DOS_LoadKeyboardLayout(temp_line.c_str(), tried_cp, cp_file_name);
1474 			} else {
1475 				keyb_error=DOS_SwitchKeyboardLayout(temp_line.c_str(), tried_cp);
1476 			}
1477 			switch (keyb_error) {
1478 				case KEYB_NOERROR:
1479 					WriteOut(MSG_Get("PROGRAM_KEYB_NOERROR"),temp_line.c_str(),dos.loaded_codepage);
1480 					break;
1481 				case KEYB_FILENOTFOUND:
1482 					WriteOut(MSG_Get("PROGRAM_KEYB_FILENOTFOUND"),temp_line.c_str());
1483 					WriteOut(MSG_Get("PROGRAM_KEYB_SHOWHELP"));
1484 					break;
1485 				case KEYB_INVALIDFILE:
1486 					WriteOut(MSG_Get("PROGRAM_KEYB_INVALIDFILE"),temp_line.c_str());
1487 					break;
1488 				case KEYB_LAYOUTNOTFOUND:
1489 					WriteOut(MSG_Get("PROGRAM_KEYB_LAYOUTNOTFOUND"),temp_line.c_str(),tried_cp);
1490 					break;
1491 				case KEYB_INVALIDCPFILE:
1492 					WriteOut(MSG_Get("PROGRAM_KEYB_INVCPFILE"),temp_line.c_str());
1493 					WriteOut(MSG_Get("PROGRAM_KEYB_SHOWHELP"));
1494 					break;
1495 				default:
1496 					LOG(LOG_DOSMISC,LOG_ERROR)("KEYB:Invalid returncode %x",keyb_error);
1497 					break;
1498 			}
1499 		}
1500 	} else {
1501 		/* no parameter in the command line, just output codepage info and possibly loaded layout ID */
1502 		const char* layout_name = DOS_GetLoadedLayout();
1503 		if (layout_name==NULL) {
1504 			WriteOut(MSG_Get("PROGRAM_KEYB_INFO"),dos.loaded_codepage);
1505 		} else {
1506 			WriteOut(MSG_Get("PROGRAM_KEYB_INFO_LAYOUT"),dos.loaded_codepage,layout_name);
1507 		}
1508 	}
1509 }
1510 
KEYB_ProgramStart(Program ** make)1511 static void KEYB_ProgramStart(Program * * make) {
1512 	*make=new KEYB;
1513 }
1514 
1515 
DOS_SetupPrograms(void)1516 void DOS_SetupPrograms(void) {
1517 	/*Add Messages */
1518 
1519 	MSG_Add("PROGRAM_MOUNT_CDROMS_FOUND","CDROMs found: %d\n");
1520 	MSG_Add("PROGRAM_MOUNT_STATUS_FORMAT","%-5s  %-58s %-12s\n");
1521 	MSG_Add("PROGRAM_MOUNT_STATUS_2","Drive %c is mounted as %s\n");
1522 	MSG_Add("PROGRAM_MOUNT_STATUS_1","The currently mounted drives are:\n");
1523 	MSG_Add("PROGRAM_MOUNT_ERROR_1","Directory %s doesn't exist.\n");
1524 	MSG_Add("PROGRAM_MOUNT_ERROR_2","%s isn't a directory\n");
1525 	MSG_Add("PROGRAM_MOUNT_ILL_TYPE","Illegal type %s\n");
1526 	MSG_Add("PROGRAM_MOUNT_ALREADY_MOUNTED","Drive %c already mounted with %s\n");
1527 	MSG_Add("PROGRAM_MOUNT_USAGE",
1528 		"Usage \033[34;1mMOUNT Drive-Letter Local-Directory\033[0m\n"
1529 		"For example: MOUNT c %s\n"
1530 		"This makes the directory %s act as the C: drive inside DOSBox.\n"
1531 		"The directory has to exist.\n");
1532 	MSG_Add("PROGRAM_MOUNT_UMOUNT_NOT_MOUNTED","Drive %c isn't mounted.\n");
1533 	MSG_Add("PROGRAM_MOUNT_UMOUNT_SUCCESS","Drive %c has successfully been removed.\n");
1534 	MSG_Add("PROGRAM_MOUNT_UMOUNT_NO_VIRTUAL","Virtual Drives can not be unMOUNTed.\n");
1535 	MSG_Add("PROGRAM_MOUNT_WARNING_WIN","\033[31;1mMounting c:\\ is NOT recommended. Please mount a (sub)directory next time.\033[0m\n");
1536 	MSG_Add("PROGRAM_MOUNT_WARNING_OTHER","\033[31;1mMounting / is NOT recommended. Please mount a (sub)directory next time.\033[0m\n");
1537 
1538 	MSG_Add("PROGRAM_MEM_CONVEN","%10d Kb free conventional memory\n");
1539 	MSG_Add("PROGRAM_MEM_EXTEND","%10d Kb free extended memory\n");
1540 	MSG_Add("PROGRAM_MEM_EXPAND","%10d Kb free expanded memory\n");
1541 	MSG_Add("PROGRAM_MEM_UPPER","%10d Kb free upper memory in %d blocks (largest UMB %d Kb)\n");
1542 
1543 	MSG_Add("PROGRAM_LOADFIX_ALLOC","%d kb allocated.\n");
1544 	MSG_Add("PROGRAM_LOADFIX_DEALLOC","%d kb freed.\n");
1545 	MSG_Add("PROGRAM_LOADFIX_DEALLOCALL","Used memory freed.\n");
1546 	MSG_Add("PROGRAM_LOADFIX_ERROR","Memory allocation error.\n");
1547 
1548 	MSG_Add("MSCDEX_SUCCESS","MSCDEX installed.\n");
1549 	MSG_Add("MSCDEX_ERROR_MULTIPLE_CDROMS","MSCDEX: Failure: Drive-letters of multiple CD-ROM drives have to be continuous.\n");
1550 	MSG_Add("MSCDEX_ERROR_NOT_SUPPORTED","MSCDEX: Failure: Not yet supported.\n");
1551 	MSG_Add("MSCDEX_ERROR_PATH","MSCDEX: Specified location is not a CD-ROM drive.\n");
1552 	MSG_Add("MSCDEX_ERROR_OPEN","MSCDEX: Failure: Invalid file or unable to open.\n");
1553 	MSG_Add("MSCDEX_TOO_MANY_DRIVES","MSCDEX: Failure: Too many CD-ROM drives (max: 5). MSCDEX Installation failed.\n");
1554 	MSG_Add("MSCDEX_LIMITED_SUPPORT","MSCDEX: Mounted subdirectory: limited support.\n");
1555 	MSG_Add("MSCDEX_INVALID_FILEFORMAT","MSCDEX: Failure: File is either no ISO/CUE image or contains errors.\n");
1556 	MSG_Add("MSCDEX_UNKNOWN_ERROR","MSCDEX: Failure: Unknown error.\n");
1557 
1558 	MSG_Add("PROGRAM_RESCAN_SUCCESS","Drive cache cleared.\n");
1559 
1560 	MSG_Add("PROGRAM_INTRO",
1561 		"\033[2J\033[32;1mWelcome to DOSBox\033[0m, an x86 emulator with sound and graphics.\n"
1562 		"DOSBox creates a shell for you which looks like old plain DOS.\n"
1563 		"\n"
1564 		"For information about basic mount type \033[34;1mintro mount\033[0m\n"
1565 		"For information about CD-ROM support type \033[34;1mintro cdrom\033[0m\n"
1566 		"For information about special keys type \033[34;1mintro special\033[0m\n"
1567 		"For more information about DOSBox, go to \033[34;1mhttp://www.dosbox.com/wiki\033[0m\n"
1568 		"\n"
1569 		"\033[31;1mDOSBox will stop/exit without a warning if an error occurred!\033[0m\n"
1570 		"\n"
1571 		"\n"
1572 		);
1573 	MSG_Add("PROGRAM_INTRO_MOUNT_START",
1574 		"\033[32;1mHere are some commands to get you started:\033[0m\n"
1575 		"Before you can use the files located on your own filesystem,\n"
1576 		"You have to mount the directory containing the files.\n"
1577 		"\n"
1578 		);
1579 	MSG_Add("PROGRAM_INTRO_MOUNT_WINDOWS",
1580 		"\033[44;1m\xC9\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
1581 		"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
1582 		"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB\n"
1583 		"\xBA \033[32mmount c c:\\dosgames\\\033[37m will create a C drive with c:\\dosgames as contents.\xBA\n"
1584 		"\xBA                                                                         \xBA\n"
1585 		"\xBA \033[32mc:\\dosgames\\\033[37m is an example. Replace it with your own games directory.  \033[37m \xBA\n"
1586 		"\xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
1587 		"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
1588 		"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC\033[0m\n"
1589 		);
1590 	MSG_Add("PROGRAM_INTRO_MOUNT_OTHER",
1591 		"\033[44;1m\xC9\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
1592 		"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
1593 		"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB\n"
1594 		"\xBA \033[32mmount c ~/dosgames\033[37m will create a C drive with ~/dosgames as contents.\xBA\n"
1595 		"\xBA                                                                      \xBA\n"
1596 		"\xBA \033[32m~/dosgames\033[37m is an example. Replace it with your own games directory.\033[37m  \xBA\n"
1597 		"\xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
1598 		"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
1599 		"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC\033[0m\n"
1600 		);
1601 	MSG_Add("PROGRAM_INTRO_MOUNT_END",
1602 		"When the mount has successfully completed you can type \033[34;1mc:\033[0m to go to your freshly\n"
1603 		"mounted C-drive. Typing \033[34;1mdir\033[0m there will show its contents."
1604 		" \033[34;1mcd\033[0m will allow you to\n"
1605 		"enter a directory (recognised by the \033[33;1m[]\033[0m in a directory listing).\n"
1606 		"You can run programs/files which end with \033[31m.exe .bat\033[0m and \033[31m.com\033[0m.\n"
1607 		);
1608 	MSG_Add("PROGRAM_INTRO_CDROM",
1609 		"\033[2J\033[32;1mHow to mount a Real/Virtual CD-ROM Drive in DOSBox:\033[0m\n"
1610 		"DOSBox provides CD-ROM emulation on several levels.\n"
1611 		"\n"
1612 		"The \033[33mbasic\033[0m level works on all CD-ROM drives and normal directories.\n"
1613 		"It installs MSCDEX and marks the files read-only.\n"
1614 		"Usually this is enough for most games:\n"
1615 		"\033[34;1mmount d \033[0;31mD:\\\033[34;1m -t cdrom\033[0m   or   \033[34;1mmount d C:\\example -t cdrom\033[0m\n"
1616 		"If it doesn't work you might have to tell DOSBox the label of the CD-ROM:\n"
1617 		"\033[34;1mmount d C:\\example -t cdrom -label CDLABEL\033[0m\n"
1618 		"\n"
1619 		"The \033[33mnext\033[0m level adds some low-level support.\n"
1620 		"Therefore only works on CD-ROM drives:\n"
1621 		"\033[34;1mmount d \033[0;31mD:\\\033[34;1m -t cdrom -usecd \033[33m0\033[0m\n"
1622 		"\n"
1623 		"The \033[33mlast\033[0m level of support depends on your Operating System:\n"
1624 		"For \033[1mWindows 2000\033[0m, \033[1mWindows XP\033[0m and \033[1mLinux\033[0m:\n"
1625 		"\033[34;1mmount d \033[0;31mD:\\\033[34;1m -t cdrom -usecd \033[33m0 \033[34m-ioctl\033[0m\n"
1626 		"For \033[1mWindows 9x\033[0m with a ASPI layer installed:\n"
1627 		"\033[34;1mmount d \033[0;31mD:\\\033[34;1m -t cdrom -usecd \033[33m0 \033[34m-aspi\033[0m\n"
1628 		"\n"
1629 		"Replace \033[0;31mD:\\\033[0m with the location of your CD-ROM.\n"
1630 		"Replace the \033[33;1m0\033[0m in \033[34;1m-usecd \033[33m0\033[0m with the number reported for your CD-ROM if you type:\n"
1631 		"\033[34;1mmount -cd\033[0m\n"
1632 		);
1633 	MSG_Add("PROGRAM_INTRO_SPECIAL",
1634 		"\033[2J\033[32;1mSpecial keys:\033[0m\n"
1635 		"These are the default keybindings.\n"
1636 		"They can be changed in the \033[33mkeymapper\033[0m.\n"
1637 		"\n"
1638 		"\033[33;1mALT-ENTER\033[0m   : Go full screen and back.\n"
1639 		"\033[33;1mALT-PAUSE\033[0m   : Pause DOSBox.\n"
1640 		"\033[33;1mCTRL-F1\033[0m     : Start the \033[33mkeymapper\033[0m.\n"
1641 		"\033[33;1mCTRL-F4\033[0m     : Update directory cache for all drives! Swap mounted disk-image.\n"
1642 		"\033[33;1mCTRL-ALT-F5\033[0m : Start/Stop creating a movie of the screen.\n"
1643 		"\033[33;1mCTRL-F5\033[0m     : Save a screenshot.\n"
1644 		"\033[33;1mCTRL-F6\033[0m     : Start/Stop recording sound output to a wave file.\n"
1645 		"\033[33;1mCTRL-ALT-F7\033[0m : Start/Stop recording of OPL commands.\n"
1646 		"\033[33;1mCTRL-ALT-F8\033[0m : Start/Stop the recording of raw MIDI commands.\n"
1647 		"\033[33;1mCTRL-F7\033[0m     : Decrease frameskip.\n"
1648 		"\033[33;1mCTRL-F8\033[0m     : Increase frameskip.\n"
1649 		"\033[33;1mCTRL-F9\033[0m     : Kill DOSBox.\n"
1650 		"\033[33;1mCTRL-F11\033[0m    : Slow down emulation (Decrease DOSBox Cycles).\n"
1651 		"\033[33;1mCTRL-F12\033[0m    : Speed up emulation (Increase DOSBox Cycles).\n"
1652 		"\033[33;1mALT-F12\033[0m     : Unlock speed (turbo button/fast forward).\n"
1653 		);
1654 	MSG_Add("PROGRAM_BOOT_NOT_EXIST","Bootdisk file does not exist.  Failing.\n");
1655 	MSG_Add("PROGRAM_BOOT_NOT_OPEN","Cannot open bootdisk file.  Failing.\n");
1656 	MSG_Add("PROGRAM_BOOT_WRITE_PROTECTED","Image file is read-only! Might create problems.\n");
1657 	MSG_Add("PROGRAM_BOOT_PRINT_ERROR","This command boots DOSBox from either a floppy or hard disk image.\n\n"
1658 		"For this command, one can specify a succession of floppy disks swappable\n"
1659 		"by pressing Ctrl-F4, and -l specifies the mounted drive to boot from.  If\n"
1660 		"no drive letter is specified, this defaults to booting from the A drive.\n"
1661 		"The only bootable drive letters are A, C, and D.  For booting from a hard\n"
1662 		"drive (C or D), the image should have already been mounted using the\n"
1663 		"\033[34;1mIMGMOUNT\033[0m command.\n\n"
1664 		"The syntax of this command is:\n\n"
1665 		"\033[34;1mBOOT [diskimg1.img diskimg2.img] [-l driveletter]\033[0m\n"
1666 		);
1667 	MSG_Add("PROGRAM_BOOT_UNABLE","Unable to boot off of drive %c");
1668 	MSG_Add("PROGRAM_BOOT_IMAGE_OPEN","Opening image file: %s\n");
1669 	MSG_Add("PROGRAM_BOOT_IMAGE_NOT_OPEN","Cannot open %s");
1670 	MSG_Add("PROGRAM_BOOT_BOOT","Booting from drive %c...\n");
1671 	MSG_Add("PROGRAM_BOOT_CART_WO_PCJR","PCjr cartridge found, but machine is not PCjr");
1672 	MSG_Add("PROGRAM_BOOT_CART_LIST_CMDS","Available PCjr cartridge commandos:%s");
1673 	MSG_Add("PROGRAM_BOOT_CART_NO_CMDS","No PCjr cartridge commandos found");
1674 
1675 	MSG_Add("PROGRAM_LOADROM_SPECIFY_FILE","Must specify ROM file to load.\n");
1676 	MSG_Add("PROGRAM_LOADROM_CANT_OPEN","ROM file not accessible.\n");
1677 	MSG_Add("PROGRAM_LOADROM_TOO_LARGE","ROM file too large.\n");
1678 	MSG_Add("PROGRAM_LOADROM_INCOMPATIBLE","Video BIOS not supported by machine type.\n");
1679 	MSG_Add("PROGRAM_LOADROM_UNRECOGNIZED","ROM file not recognized.\n");
1680 	MSG_Add("PROGRAM_LOADROM_BASIC_LOADED","BASIC ROM loaded.\n");
1681 
1682 	MSG_Add("PROGRAM_IMGMOUNT_SPECIFY_DRIVE","Must specify drive letter to mount image at.\n");
1683 	MSG_Add("PROGRAM_IMGMOUNT_SPECIFY2","Must specify drive number (0 or 3) to mount image at (0,1=fda,fdb;2,3=hda,hdb).\n");
1684 	MSG_Add("PROGRAM_IMGMOUNT_SPECIFY_GEOMETRY",
1685 		"For \033[33mCD-ROM\033[0m images:   \033[34;1mIMGMOUNT drive-letter location-of-image -t iso\033[0m\n"
1686 		"\n"
1687 		"For \033[33mhardrive\033[0m images: Must specify drive geometry for hard drives:\n"
1688 		"bytes_per_sector, sectors_per_cylinder, heads_per_cylinder, cylinder_count.\n"
1689 		"\033[34;1mIMGMOUNT drive-letter location-of-image -size bps,spc,hpc,cyl\033[0m\n");
1690 	MSG_Add("PROGRAM_IMGMOUNT_INVALID_IMAGE","Could not load image file.\n"
1691 		"Check that the path is correct and the image is accessible.\n");
1692 	MSG_Add("PROGRAM_IMGMOUNT_INVALID_GEOMETRY","Could not extract drive geometry from image.\n"
1693 		"Use parameter -size bps,spc,hpc,cyl to specify the geometry.\n");
1694 	MSG_Add("PROGRAM_IMGMOUNT_TYPE_UNSUPPORTED","Type \"%s\" is unsupported. Specify \"hdd\" or \"floppy\" or\"iso\".\n");
1695 	MSG_Add("PROGRAM_IMGMOUNT_FORMAT_UNSUPPORTED","Format \"%s\" is unsupported. Specify \"fat\" or \"iso\" or \"none\".\n");
1696 	MSG_Add("PROGRAM_IMGMOUNT_SPECIFY_FILE","Must specify file-image to mount.\n");
1697 	MSG_Add("PROGRAM_IMGMOUNT_FILE_NOT_FOUND","Image file not found.\n");
1698 	MSG_Add("PROGRAM_IMGMOUNT_MOUNT","To mount directories, use the \033[34;1mMOUNT\033[0m command, not the \033[34;1mIMGMOUNT\033[0m command.\n");
1699 	MSG_Add("PROGRAM_IMGMOUNT_ALREADY_MOUNTED","Drive already mounted at that letter.\n");
1700 	MSG_Add("PROGRAM_IMGMOUNT_CANT_CREATE","Can't create drive from file.\n");
1701 	MSG_Add("PROGRAM_IMGMOUNT_MOUNT_NUMBER","Drive number %d mounted as %s\n");
1702 	MSG_Add("PROGRAM_IMGMOUNT_NON_LOCAL_DRIVE", "The image must be on a host or local drive.\n");
1703 	MSG_Add("PROGRAM_IMGMOUNT_MULTIPLE_NON_CUEISO_FILES", "Using multiple files is only supported for cue/iso images.\n");
1704 
1705 	MSG_Add("PROGRAM_KEYB_INFO","Codepage %i has been loaded\n");
1706 	MSG_Add("PROGRAM_KEYB_INFO_LAYOUT","Codepage %i has been loaded for layout %s\n");
1707 	MSG_Add("PROGRAM_KEYB_SHOWHELP",
1708 		"\033[32;1mKEYB\033[0m [keyboard layout ID[ codepage number[ codepage file]]]\n\n"
1709 		"Some examples:\n"
1710 		"  \033[32;1mKEYB\033[0m: Display currently loaded codepage.\n"
1711 		"  \033[32;1mKEYB\033[0m sp: Load the spanish (SP) layout, use an appropriate codepage.\n"
1712 		"  \033[32;1mKEYB\033[0m sp 850: Load the spanish (SP) layout, use codepage 850.\n"
1713 		"  \033[32;1mKEYB\033[0m sp 850 mycp.cpi: Same as above, but use file mycp.cpi.\n");
1714 	MSG_Add("PROGRAM_KEYB_NOERROR","Keyboard layout %s loaded for codepage %i\n");
1715 	MSG_Add("PROGRAM_KEYB_FILENOTFOUND","Keyboard file %s not found\n\n");
1716 	MSG_Add("PROGRAM_KEYB_INVALIDFILE","Keyboard file %s invalid\n");
1717 	MSG_Add("PROGRAM_KEYB_LAYOUTNOTFOUND","No layout in %s for codepage %i\n");
1718 	MSG_Add("PROGRAM_KEYB_INVCPFILE","None or invalid codepage file for layout %s\n\n");
1719 
1720 	/*regular setup*/
1721 	PROGRAMS_MakeFile("MOUNT.COM",MOUNT_ProgramStart);
1722 	PROGRAMS_MakeFile("MEM.COM",MEM_ProgramStart);
1723 	PROGRAMS_MakeFile("LOADFIX.COM",LOADFIX_ProgramStart);
1724 	PROGRAMS_MakeFile("RESCAN.COM",RESCAN_ProgramStart);
1725 	PROGRAMS_MakeFile("INTRO.COM",INTRO_ProgramStart);
1726 	PROGRAMS_MakeFile("BOOT.COM",BOOT_ProgramStart);
1727 	PROGRAMS_MakeFile("LOADROM.COM", LOADROM_ProgramStart);
1728 	PROGRAMS_MakeFile("IMGMOUNT.COM", IMGMOUNT_ProgramStart);
1729 	PROGRAMS_MakeFile("KEYB.COM", KEYB_ProgramStart);
1730 }
1731