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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! MS-DOS MZ Executable Format
//!
//! The MS-DOS-MZ-Executable format was introduced with Microsoft DOS-2.0 and
//! replaced the plain COM-format. It defines how an executable is to be loaded
//! into memory, its required relocations, and the initial register state when
//! starting execution. Its name was derived from the signature used by the
//! format.
//!
//! The format is rarely used today. Its most common use is as part of stub
//! programs embedded in its successor formats like PE (Portable Executable).
//!
//! Applications linked in the original MS-DOS format are often called "DOS COM
//! Programs", while applications linked with the format described here are
//! commonly referred to as "DOS EXE Programs".
//!
//! The format is highly tied to how programs were executed on old x86 machines
//! running the DOS operating system. Memory segmentation in particular plays
//! an important role in many of the design decisions. A full understanding of
//! the MS-DOS MZ Executable Format requires at least basic understanding of
//! how MS-DOS works. Followingly, a list of notes that might be of value to
//! understand the intricacies of MS-DOS executables:
//!
//! * Memory was organized in 64KiB segments, but segments are overlapping and
//! offset by 16 bytes from each other. Hence, the total addressable memory
//! is 1MiB (actually based on the 20-bit address-bus used in old x86
//! hardware).
//! Due to architectual decisions, only the lower 640KiB were allocated to
//! the application, while the upper memory was reserved for video hardware
//! or other hardware management.
//! Newer hardware supported all kinds of extensions to provide access to
//! memory beyond 1MiB, yet those extensions have little effect on the
//! design decisions of this format and thus are mostly ignored here.
//!
//! * Original DOS COM applications had a hard-coded entry-point at 0x100 and
//! only supported addressing a single segment (64KiB). The new DOS EXE
//! applications support applications spanning multiple segments, as well as
//! any entry-point.
//! The initial offset of 0x100 was due to the Program Segment Prefix (PSP)
//! being placed in front of the application. This structure contained
//! information for the application, including its environment and
//! command-line arguments.
//!
//! * While MS-DOS had different styles of programs running in parallel to
//! serve hardware interrupts and manage the system, it effectively was a
//! single-tasking OS that only ran a single application at once. However,
//! it is common for programs to load other programs and resume execution
//! once this program returned. Thus, multiple programs will be loaded in
//! memory at a time.
//!
//! * On application startup, the loader calculates the size of the program to
//! load into memory, as well as the minimum required size of additional
//! memory beyond that, as noted in the file header. It then tries to find
//! the biggest block of memory that fulfils that need, up to a maximum as
//! specified in the file header again. This allows applications to specify
//! a minimum required space beyond the code stored in the file, as well as
//! ask for additional optional memory serving as heap memory.
//!
//! * A new application is loaded into memory with its PSP directly in front
//! of it. The start-segment is the first segment after the PSP, and thus
//! the first segment holding the application. The start-segment is used to
//! relocate DOS EXE executables, and allows executables to be loaded at any
//! segment, while still spanning multiple segments.
//!
//! * Old DOS executables assumed that any higher segment than their start
//! segment is also available to them, and thus assumed anything in the
//! range [CS:0x0000; 0xa000:0x0000] (i.e., anything from the start-segment
//! up to the highest segment at 640KiB) is available to them. This is not
//! strictly true, though, since high memory can be reserved for other
//! reasons. Hence, an application can query the PSP for the first segment
//! beyond the area allocated to the application to get an authoritative
//! answer.
//!
//! * While dealing with old MS-DOS formats, there are 3 important sizes used
//! to measure memory areas:
//!
//! * `WORD`: A word is a 16-bit integer. `DWORD` and `QWORD` refer to
//! double and quadruple of that size.
//!
//! * `PARAGRAPH`: A paragraph are 16 consecutive bytes. This is most often
//! used to allocate blocks of memory, or perform operations on large
//! blocks. The size reflects the offset between segments on x86.
//!
//! * `PAGE`: A page is 512 consecutive bytes. This is not to be confused
//! with page-sizes on modern x86 hardware (often 4k).
//!
//! * All multi-byte integers are encoded as little-endian.
//!
//! The file-format is rather trivial. It consists of a static header, which
//! embeds its own size and thus allows for applications to store additional
//! private data trailing the static header. Additionally, the file contains a
//! relocation table with all address-relocations that are to be applied to the
//! application code before execution. This table is usually placed directly
//! after the header, including its size in the total header size.
//!
//! Anything beyond the header is considered application code, up to the total
//! size as specified in the header. Anything trailing that is not considered
//! part of the executable and ignored.
//!
//! There is a Microsoft header extension which is used to designate the file
//! as a DOS-compatibility executable. This header must directly follow the
//! static header, and the relocation-offset usually indicates that the
//! relocation table follows this extended header, thus showing that such an
//! extended header is likely present. The extended header does not affect the
//! executable, but contains metadata describing other data stored after the
//! code (which is ignored by the DOS EXE loader). This technique is usually
//! used to combine a DOS EXE executable with a more modern PE executable into
//! a single file.
use ;
type U16Le = Integer;
type U32Le = Integer;
/// Size of a Word
///
/// The term `WORD` is used to denote 2-byte integers in MS-DOS software, and
/// many derivatives. It reflects the size of the data-bus and registers of the
/// originally supported x86 machines.
pub const WORD_SIZE: usize = 2;
/// Size of a Paragraph
///
/// The term `PARAGRAPH` is used to denote 16-byte memory regions in MS-DOS
/// software and some derivatives. It reflects the offset of two consecutive
/// segments on x86 machines in Real Mode. Note that segments are overlapping,
/// thus a paragraph describes their relative distance but not their size.
pub const PARAGRAPH_SIZE: usize = 16;
/// Size of a Page
///
/// The term `PAGE` usually denotes 512-byte memory regions in MS-DOS software.
/// It reflects the size of hardware pages used to store memory on disk. Hence,
/// for better performance, in-memory data is often also organized in pages to
/// allow easy mapping to storage pages.
pub const PAGE_SIZE: usize = 512;
/// Magic Signature
///
/// The initial 2 bytes of every DOS-MZ file are set to `0x4d` followed by
/// `0x5a` ("MZ"). They are used to identify the format. They reflect the
/// initials of an early Microsoft employee who worked on the format.
pub const MAGIC: = ;
/// Calculate 16-bit Sum
///
/// This function splits a byte slice into consecutive 16-bit unsigned integers
/// and calculates their sum. Little endianness is assumed. If the slice is of
/// an odd length, a missing trailing zero byte is assumed.
///
/// The sum is calculated in wrapping-mode, meaning overflows are handled
/// gracefully.
///
/// This function is tailored for checksum calculations, which commonly are
/// based on the 16-bit sum of a byte slice.
/// File Header
///
/// This static structure is located at offset 0 of a DOS MZ executable. It
/// has a fixed size of 28 bytes and describes the further layout of the file.
/// Extended Header
///
/// The extended header optionally follows the static header without any
/// padding. The presence of an extended header is suggested by the relocation
/// offset being beyond the extended header, as well as the header size being
/// big enough to include the extended header.
///
/// The only meaningful field of the extended header is `lfanew`, which is a
/// 32-bit offset into the file where further header information can be found.
/// Depending on the format that uses this extended header, a different
/// signature can be found at that offset.
///
/// The other fields of this extended header are very scarcely documented and
/// thus usually set to 0.
/// Relocation Information
///
/// This structure describes an entry of the relocation table. Each entry
/// points into the program code, at a 2-byte value that must be adjusted with
/// the start-segment before the program is run. The value of the start-segment
/// is simply added to each location pointed at by the relocation table.
///
/// A single location is described by its segment relative to the start of the
/// program, as well as the offset inside that segment.
/// X86 Stub Program
///
/// This array contains a full MS-DOS EXE program that prints the following
/// line on startup and then exits with an error code of 1:
///
/// "This program cannot be run in DOS mode.\r\r\n"
///
/// A stub program like this is typically used with extended file-formats like
/// PE/COFF.
///
/// This stub is a fully functioning DOS program of size 128 bytes. It contains
/// an extended DOS header with the `lfanew` offset set to 128 (directly after
/// this stub). Hence, you can prepend this 128-byte stub to any PE program
/// without any modifications required. If required, the `lfanew` offset can
/// be adjusted after copying it.
pub const STUB_X86: = ;