Commit graph

383 commits

Author SHA1 Message Date
Timo Kreuzer
9b8c59d6b7 [FREELDR] Refactor LoadBootDeviceDriver 2024-11-02 17:31:57 +02:00
Timo Kreuzer
393b1f8998 [FREELDR] Implement PeLdrLoadBootImage 2024-11-02 17:31:57 +02:00
Timo Kreuzer
f9849f7cc4 [FREELDR] Initialize a global module list head for freeldr 2024-11-02 17:31:57 +02:00
Timo Kreuzer
6eac9b5891 [FREELDR] Pass VA instead of PA to PeLdrAllocateDataTableEntry
This allows to create DTEs for loader modules.
2024-11-02 17:31:57 +02:00
Timo Kreuzer
8a5ef4c1cc [FREELDR] Implement PeLdrLoadImageEx
This allows to load an image as freeldr extension code.
TODO:
- Add global bootloader DTE list
- Add wrapper function that also processes imports
- Use this for scsiport
2024-11-02 17:31:57 +02:00
Timo Kreuzer
bee8e81d5a [FREELDR] Move IoReadPartitionTable to ntoskrnl.c 2024-11-02 17:31:57 +02:00
Timo Kreuzer
77ba2d496b [FREELDR] Fix some trace prints 2024-11-02 17:31:57 +02:00
Daniel Victor
3fd6e34ab8
[FREELDR/x64] Fix the Multiboot pointer size (#7469)
Addendum to b524c18a3e. CORE-18374
2024-10-19 18:50:41 +03:00
Daniel Victor
b524c18a3e [FREELDR/x64] Add Multiboot header 2024-10-19 17:54:39 +03:00
Timo Kreuzer
bed8b2a0a6 [FREELDR][NDK][X64] Remove KIP0PCRADDRESS definition and mapping
It is not used by either ReactOS or Windows.
2024-10-17 18:31:32 +03:00
Hermès Bélusca-Maïto
5d361b602d
[FREELDR] fs.c: Fix handling of file handles, (de)referencing, and direct-device access (#7414)
- The original code was just incrementing the reference counts (RefCounts)
  of the device objects or the device/file handles, without decrementing
  them when closing the handles. This is now fixed.

  Notice the following:

  * When opening a file on a device (disk), the device's (and its
    handle's) RefCount is incremented, and the file handle's RefCount
    is incremented as well.

  * When closing a file, the file handle's RefCount is decremented
    (and the file closed if the RefCount reaches zero), and the file's
    parent device handle is also closed, recursively.
    This has the effect of decrementing the parent device handle's
    RefCount, and the device's own RefCount is decremented as well.

  IMPORTANT NOTE: The usefulness of handle-level RefCount is still
  under question, and might be (consistently) removed in the future.

- Fix opening a device (disk) in direct access, when this device is
  already opened. Indeed, we previously allowed direct access only if
  the device was opened as such for the very first time (its RefCount
  = 0 originally); no filesystem mounting was attempted as well.
  Then for any later open-operations on this device (while keeping an
  already-opened handle to it), filesystem access was assumed.

  Thus, this problem would show up in two ways:

  * Either the device is first opened for direct access, this succeeded
    and no filesystem was mounted. Then, for any other open-operations,
    the filesystem was NOT mounted, and opening files on it would fail.
    Direct accesses would succeed but would create an unnecessary second
    file handle.

  * Or, the device is first opened for file-access: a filesystem was
    mounted and file opening would succeed. Any other file opening
    operation would succeed as well (if the file exists). But, a direct
    access open-operation would fail, because now any open-operations on
    the device would be assumed to be a file opening.

  This is now correctly fixed. If direct-open is requested, just do it.
  If this is a file opening, we open the device, then try to mount a
  filesystem on it (if not already done), then we try to open the file.

  If file opening fails, derereference the device.

- Pass the file path to the filesystem-specific Open() functions without
  truncating the leading path separator, if any. This has to be handled
  by the filesystem routines themselves.
2024-10-16 22:20:52 +02:00
Joachim Henze
798ea90784
[FREELDR] Less outdated version-hardcodes (#7384)
Those haven't been groomed anymore for more than 10 years. We had many thousands of
different freeldr builds with different behavior and bugs each, but nobody ever
did have the slightest motivation to update those hardcoded
FREELOADER_MAJOR_VERSION, FREELOADER_MINOR_VERSION, FREELOADER_PATCH_VERSION
from ver.h. And that is logical, because touching other modules will change the behavior
of freeldr as well, so it is absolutely impossible to groom anything like that correctly.
Instead we should simply do what we started to do in PR7383, which will at least give
some information (the actual sources it was built from) instead of some misleading voodoo-version.

This might slightly shrink the size of freeldr as well, but I was too lazy to measure by how much.
2024-10-13 19:23:38 +02:00
Timo Kreuzer
0c2cdcae83 [REACTOS] Add missing line breaks at end of file 2024-10-06 10:47:11 +03:00
Hermès Bélusca-Maïto
764881a94b
[FREELDR] Remove the "setupldr" build target (#7419)
The setupldr and freeldr binaries are the same since commit 948e82af1
(r65832), because both their functionality have been shared.
2024-10-03 12:32:45 +02:00
Hermès Bélusca-Maïto
3b928898ef
[FREELDR] Simplify some control branches in loops (#7417)
Collapse some tests; remove redundant `continue;`

Pointed out by Serge Gautherie.
2024-10-03 11:47:31 +02:00
Hermès Bélusca-Maïto
3dfbe52699
[FREELDR] fs.c: Minor refactoring in ArcOpen(); fix a memory leak (#7385)
- `ArcOpen()`: flatten the registered-device search for-loop.
  Limit it to just the device search, and exit early.
  The rest of the initialization is now done outside the loop.

- The `DeviceName` string pointer may have been allocated from
  the heap, for path normalization (see `NormalizeArcDeviceName()`).
  This pointer is then only used in the device search loop and not
  nused anymore afterwards. The old code didn't free this pointer
  and memory could leak. This is now fixed easily, thanks to the
  loop flattening.

- Rename `DEVICE` member `Prefix` to `DeviceName`; SAL-annotate
  `FsRegisterDevice()`.
2024-10-01 22:15:11 +02:00
Hermès Bélusca-Maïto
689b9e0475
[FREELDR] fs.c: Move the "()" -> "(0)" ARC path normalization code into a function (#7385)
This function will be useful in other places later.
2024-10-01 22:14:02 +02:00
Hermès Bélusca-Maïto
829ad06179
[FREELDR] fs.c: Simplify FileId checks; add missing DeviceId invalidation (#7385)
- Replace a lot of `MAX_FDS` by `_countof(FileData)`;

- The duplicated FileId validation logic is wrapped with
  the `IS_VALID_FILEID()` macro.

- When returning an invalid FileId value on purpose, use
  `INVALID_FILE_ID` instead of `MAX_FDS` (that could vary
  if the file handle table gets extended).
  And replace the `(ULONG)-1` also used for that purpose
  by `INVALID_FILE_ID`.

- Add missing DeviceId invalidation:
  * when failing to open a file handle in `ArcOpen()`;
  * when registering a new device in `FsRegisterDevice()`.
    There, having DeviceId always set to zero would tell
    the code that the corresponding device's file ID is
    the first one in the table, which is a BUG. (Many devices
    would have the same file ID...)

- In addition: massage a bit some indicial for-loops.
2024-10-01 22:14:01 +02:00
Hermès Bélusca-Maïto
29a0ff73e6
[FREELDR] fs.c: Move the filesystem mount routines list into a table (#7385)
This allows to make the code better extendable: adding a new FS
mount routine into the table, instead of duplicating also a whole
`if (!FileFuncTable) ...` check.

Later, this table will be made dynamic, so that new filesystems could be
dynamically registered at runtime, and a filesystem could be forced to
be mounted by the user (using a specific syntax).
2024-10-01 22:13:51 +02:00
Hermès Bélusca-Maïto
3a7fe56a5d
[FREELDR] Change GEOMETRY "Sectors" to "SectorsPerTrack" and introduce a new "Sectors" field (#7379)
----
Thanks to the following testers!
- Dmitry Borisov (@disean) for testing on NEC PC-98 emulator;
- Justin Miller (@DarkFire01) for testing on UEFI platform;
- Stanislav Motylkov (@binarymaster) for testing on Xbox emulator
  (xemu), both livecd and bootcd.
----

"SectorsPerTrack" is for the legacy Cylinders/Heads/Sectors(PerTrack)
scheme.

- On BIOS-based PCs, INT 13h can return (for LBA-only drives) an invalid
  geometry, like: C/H/S = (-1)/(-1)/(-1). This is also what happens in
  our hwide.c driver (see IdentifyDevice() for ATAPI devices):
db419efbf2/boot/freeldr/freeldr/arch/drivers/hwide.c (L918-L928)

  as well as on VirtualBox for CD-ROMs:
https://www.virtualbox.org/browser/vbox/trunk/src/VBox/Devices/PC/BIOS/disk.c#L155

- Therefore, we cannot reliably calculate a valid total number of sectors
  by multiplying the Cylinders*Heads*SectorsPerTrack values. In addition,
  such a multiplication could overflow a 32-bit ULONG.
  Thus, a separate ULONGLONG Sectors member is required to hold such a
  value, that is retrieved differently. For example for ATAPI devices,
  our hwide.c driver does return a valid TotalSectors value, even though
  CHS values are invalid. Other platforms, like UEFI, just work using
  logical block addressing (LBA) values (see EFI_BLOCK_IO_MEDIA).

- uefidisk.c : Per the spec, EFI_BLOCK_IO_MEDIA::LastBlock contains
  "The last LBA on the device. [...] For ATA devices, this is reported
  in IDENTIFY DEVICE data words 60-61 (i.e., Total number of user
  addressable logical sectors) _minus one_.
  For SCSI devices, this is reported in the READ CAPACITY parameter
  data 'Returned Logical Block Address field' _minus one_."
  In other words, LastBlock is a zero-based LBA index quantity. The
  corresponding total number of valid "sectors"/blocks of the device
  is therefore, (LastBlock + 1).

- Cleanup some old disabled code.
2024-09-30 12:11:15 +02:00
Joachim Henze
dbda7ab66f
[FDEBUG] *.rc: Less outdated version-hardcodes (#7383)
We do have 22 rc files. Strip a hardcoded version in all of them.
Which hasn't been groomed by anyone for longer than the year 2005 already (SVN r18478).
What a waste of bytes!
2024-09-28 21:23:28 +02:00
Joachim Henze
4f4be5c498
[FREELDR] Print arch + buildnumber on crash-screen (#7382)
This commit tries to improve the quality of the screenshots that we do get posted in JIRA,
where we couldn't even see up to now which build and arch was run.
This is in the wider context of CORE6762
(I intentionally left out the minus in the CORE-ID to prevent auto-linkage to that ticket)
2024-09-26 00:49:29 +02:00
Hermès Bélusca-Maïto
840d39b9d0
[FREELDR] i386/pc/pcdisk.c: Fix LBA reads retry loop (#7367)
When the Int 13h AH=42h "Extended read" function fails, the disk address
packet's LBA block count is reset to the number of blocks that have been
successfully transferred. This is more or less fine, unless one wants to
ensure the exact number of sectors gets read.

If the function fails so that zero sectors were read, the retry loop is
restarted, but with the packet's LBA block count member reset, as per
the documentation. (In this example, it is reset to zero.) Then, at the
next retry attempt, zero sectors are requested to be read, and this time
of course, the call succeeds... Wrongly, of course, this is not what's
expected.

Therefore, for each retry, the LBA block count member should be set
again to the correct number of sectors to read. There are maximum 3
retries, so the retry loop will stop anyway, but the LBA read will now
correctly fail and return FALSE, as expected.

This problem doesn't exist in the retry loop for the Int 13h, AH=02h
"Read Disk Sectors" CHS function, because here, the call is made only
using registers, and we use a pair of RegsIn/RegsOut. RegsOut receives
the modified register values, but the input RegsIn stays unchanged.
2024-09-25 13:24:08 +02:00
Hermès Bélusca-Maïto
4190b48924
[FREELDR] Fix the seg:off values when dumping the extended drive parameters (#7367)
(By the way, it's "EDD": "Enhanced Disk Drive", not "EED"...)

The 13th and 14th USHORTs (at offsets 0x1A-0x1D) in the retrieved buffer
from the INT 13h, AH=48h "Get Extended Drive Parameters" function,
correspond respectively to the offset and the segment of the "EDD
configuration parameters", see http://www.ctyme.com/intr/rb-0715.htm

Fixes code introduced in commit b3f11cfb38 (r17484).

----

16 years ago, these values were wrongly stored in the returned buffer
by VirtualBox, see https://www.virtualbox.org/ticket/2848 .
This has been fixed since VBox 2.1.0 in commit 15712 (22 Dec. 2008):
https://www.virtualbox.org/changeset/15712/vbox
This problem was also noticed earlier (07 Mar. 2008) and fixed in Xen:
https://lists.xenproject.org/archives/html/xen-devel/2008-03/msg00229.html

This bug originated from Bochs, from which the two projects above
adapted their rombios.c code. It was fixed on 08-09 Oct. 2007 by
Myles Watson, see https://sourceforge.net/p/bochs/mailman/message/13777090/
and included in Bochs 1.15x and 1.185+
https://sourceforge.net/p/bochs/mailman/message/12953093/
https://sourceforge.net/p/bochs/mailman/message/12953094/
2024-09-25 13:05:07 +02:00
Hermès Bélusca-Maïto
e2d0c7de30
[FREELDR] iso.c: Perform extra validation before mounting the ISO filesystem (#7367)
Validate the primary volume descriptor version and its reported
logical block size.
2024-09-25 13:05:02 +02:00
Hermès Bélusca-Maïto
56eede6e38
[FREELDR] iso.h: Fix definition of the "Primary Volume Descriptor" PVD structure (#7367)
For reference, see the cdfs/cd.h RAW_ISO_VD structure.

Each of the old "VolumeSetSize", "VolumeSequenceNumber" and "LogicalBlockSize"
ULONG fields actually correspond to pairs of USHORT fields:
"VolumeSetSizeL" and "VolumeSetSizeM", etc., where the "L" one contains
the value in little-endian format, while the "M" one contains the same
value in big-endian format.

Additionally, use UCHARs for the character arrays.
2024-09-25 12:56:47 +02:00
Hermès Bélusca-Maïto
5d99a70597
[FREELDR] Minimally reformat include/fs/iso.h 2024-09-25 12:56:46 +02:00
Laura Konopinska
e2fc578f6d [FREELDR] Support drives with 4k sector size
PR was reviewed at pull/5784
2024-09-12 20:53:10 -07:00
Adam Słaboń
e168d60bc5
[FREELDR] Fix boot timeout regression on AMD64 (#7281)
Initialize the BootMgrInfo struct globally, so the TimeOut value is guaranteed to be negative when no Multiboot cmdline is provided. This could happen when BootMain is called with a NULL pointer:
https://git.reactos.org/?p=reactos.git;a=blob;f=boot/freeldr/freeldr/arch/amd64/entry.S;hb=163f3407c8fa93ce06773fdf9fc53064506bd05e#l73
so CmdLine == NULL and LoadSettings is called with NULL, thus the CmdLineParse isn't run.

Fixes boot timeout regression introduced in commit 7bee32d237 which occured only on AMD64 builds.
2024-09-01 11:54:28 +03:00
Serge Gautherie
44662eaf62 [CMAKE] Use COMPILE_OPTIONS instead of superseded COMPILE_FLAGS
for set_source_files_properties().
2024-08-21 11:34:48 +03:00
Hermès Bélusca-Maïto
565a4b359a
[FREELDR] Don't popup about deprecated features in auto-boot scenario (#6803)
Addendum to commit 5f3554a40.
2024-05-04 16:19:22 +02:00
Hermès Bélusca-Maïto
7bee32d237
[FREELDR] The multiboot command-line and FREELOADER section options become global settings (#6803)
Dual-license settings.c: "old" code (CmdLineParse) stays BSD
and new code (mine) becomes MIT.
2024-05-04 16:19:07 +02:00
Hermès Bélusca-Maïto
2f4bb4084d
[FREELDR] Rename cmdline.c to settings.c: it will be used for managing global settings (#6803) 2024-05-04 15:46:06 +02:00
Hermès Bélusca-Maïto
5f3554a40c
[FREELDR] Merge boot-drive and partition functionalities together (#6760)
And deprecate corresponding boot types "Drive" and "Partition".
These are replaced by the more general "BootSector" boot type.

Finish the unification of the code, started in commit ff85aa0c3,
that loads and boots disk MBR, partition VBR or boot sector in file.

A "WarnDeprecated()" helper is added to warn the user about the
deprecated features, and to inform them to adjust their FREELDR.INI
file in accordance.

In addition, bump FreeLoader version to 3.2 (at last!): a lot of
features have been added or deprecated since its last release.
2024-04-19 21:45:41 +02:00
Mark Harmstone
dd6c1c8843 [FREELDR] Allow Freeloader to boot Vista revamp of PR #1905 (#6479)
[FREELDR] Add "WindowsVista" boot type
[FREELDR] Set GDT correctly for Vista
[FREELDR] Map first page of memory, this is an observed behavior, and
also increases stability boot Checked windows 2003 SP2 ntoskrnl with
freeloader.
[SDK] Don't assert on big keys in bootloader

Co-authored-by: Justin Miller <justin.miller@reactos.org>
2024-04-18 09:28:54 -07:00
Hermès Bélusca-Maïto
7b4f42470f
[FREELDR] Reformat the boot editor strings in the code. 2024-04-17 22:25:14 +02:00
Hermès Bélusca-Maïto
e5517176b8
[FREELDR] linuxboot: It doesn't need the BIOS boot drive number and partition.
Also document a little bit the RootDevice field in the boot sector;
add SAL2 annotations.
2024-04-17 22:25:13 +02:00
Hermès Bélusca-Maïto
c25a0e1919
[FREELDR:UI] Minor miscellaneous fixes.
- UiMessageBox(): Enlarge the default buffer used to printf msgbox strings.

- TuiUpdateDateTime(): When displaying the time, don't pad too much
  with spaces on the left.

- TuiDrawShadow():
  * Pre-calculate whether we need to show the right/bottom shadows,
    and whether the right-shadow has a double width.
  * Cap the right and bottom upper-bound coordinates before looping,
    in order to avoid buffer overflows if the given coordinates go
    beyond the screen.

- TuiDrawMsgBoxCommon(): Improve how the message box centering
  calculations are done:
  * When the "full-UI" is used (and center-menu is used), try to
    center the message box in the space between the header and
    the status bar.
  * Ensure the top-left box corner is inside the screen, in case
    the calculated message box borders go off-screen.

- TuiCalcMenuBoxSize():
  * Uniformize the way the menu box coordinates are calculated.
  * Reduce the space between menu box margins and the longest item.
  * Ensure the top-left menu corner is inside the screen, in case
    the calculated menu box borders go off-screen.
2024-04-17 22:19:43 +02:00
Hermès Bélusca-Maïto
c044201472
[FREELDR] Skip NULL-pointer entries in Argv when enumerating arguments.
A NULL pointer (not necessarily the terminating one) is a valid entry
in the Argv argument vector, according to the ARC specification
(Section 4.4 "Loaded-Program Conventions").
Thus, such pointer needs to be ignored when searching over the
argument vector.
2024-04-17 21:57:12 +02:00
Hermès Bélusca-Maïto
93245d385d
[FREELDR] arcsupp.c: Relicense my code to MIT and use SAL2 annotations. 2024-04-17 21:57:11 +02:00
Hermès Bélusca-Maïto
5b2dcdd03d
[FREELDR] BuildArgvForOsLoader: Add a terminating NULL pointer to the Argv vector.
Addendum to commits d05be0da3 and bd451f240.

This is required for POSIX compliance, which the ARC specification obeys.

See Section 2.1.2.2.1 (ANSI X3.159-1989) or 5.1.2.2.1 (ISO/IEC 9899:x)
"Program startup":
```
If they are declared, the parameters to the main function shall obey
the following constraints:
— The value of argc shall be nonnegative.
— argv[argc] shall be a null pointer.
[...]
```

See also https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
2024-04-17 21:57:09 +02:00
Timo Kreuzer
43fc73207d [FREELDR/x64] Set up CR0/CR4 for SSE instructions
Newer GCC starts emitting SSE/SSE2 instructions, which would cause a triple fault, during early boot, if not enabled.
2024-03-29 19:39:01 +01:00
Adam Słaboń
fec827eeef
[FREELDR][NTOS:MM] Add security cookie generation to FreeLoader (#6270)
* [NTOS:MM] Misc improvements for cookie generation code

- Improve support for 64 bit images
- Improve LdrpFetchAddressOfSecurityCookie code

* [FREELDR] Add security cookie generation to FreeLoader

CORE-17808
2024-03-27 23:33:06 +02:00
Hermès Bélusca-Maïto
9ae73010c2
[FREELDR] Factor duplicated code into a GetOSLoadingMethod() helper
This removes duplicated code present in both LoadOperatingSystem()
and EditOperatingSystemEntry().

+ Add SAL annotations to the related functions.
2024-03-12 17:15:02 +01:00
Hermès Bélusca-Maïto
bbce6c3fdf
[FREELDR] Deduplicate common message-box creation code.
Also, make the (T)uiSave/RestoreScreen() helpers manage themselves the
temporary memory buffer used to store the temporary screen snapshot.
2024-03-11 22:37:39 +01:00
Hermès Bélusca-Maïto
64155019df
[FREELDR] Minor formatting for MsgBox functions + add SAL annotations 2024-03-11 22:37:38 +01:00
Hermès Bélusca-Maïto
dad056e06a
[FREELDR] Move the arch-specific intrin_i.h inclusions to where they are actually needed (NTLDR) 2024-03-11 22:37:37 +01:00
Hermès Bélusca-Maïto
50e5f76212
[FREELDR] FrLdrHeapCreate: Show the memory type in the ERR(). 2024-03-11 22:37:36 +01:00
Hermès Bélusca-Maïto
9a707af2e3
[FREELDR] Initialize CandidatePageCount to zero, needed if the while-loop is not run.
Granted, if the while-loop is not run, this means there is no memory
available at all on the computer where FreeLdr is running, which is
quite unexpected. But do the initialization anyways, in order to avoid
more obscure bugs down the line.

On the other hand, PageLookupTableMemAddress doesn't need to be
pre-initialized, since it gets initialized unconditionally afterwards.
2024-03-11 22:37:35 +01:00
Hermès Bélusca-Maïto
61c4b5f615
[FREELDR] pcat.cmake: minor cleanup 2024-03-11 22:37:34 +01:00