file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. file.c
  9. Abstract:
  10. This module implements file system support for the boot library.
  11. Author:
  12. Evan Green 19-Feb-2014
  13. Environment:
  14. Boot
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <minoca/kernel/kernel.h>
  20. #include <minoca/lib/fat/fat.h>
  21. #include "firmware.h"
  22. #include "bootlibp.h"
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. //
  27. // ------------------------------------------------------ Data Type Definitions
  28. //
  29. //
  30. // ----------------------------------------------- Internal Function Prototypes
  31. //
  32. KSTATUS
  33. BopOpenVolume (
  34. HANDLE DiskHandle,
  35. PBOOT_VOLUME *VolumeHandle
  36. );
  37. //
  38. // -------------------------------------------------------------------- Globals
  39. //
  40. //
  41. // ------------------------------------------------------------------ Functions
  42. //
  43. KSTATUS
  44. BoOpenBootVolume (
  45. ULONG BootDriveNumber,
  46. ULONGLONG PartitionOffset,
  47. PBOOT_ENTRY BootEntry,
  48. PBOOT_VOLUME *VolumeHandle
  49. )
  50. /*++
  51. Routine Description:
  52. This routine opens a handle to the boot volume device, which is the device
  53. this boot application was loaded from.
  54. Arguments:
  55. BootDriveNumber - Supplies the drive number of the boot device, for PC/AT
  56. systems.
  57. PartitionOffset - Supplies the offset in sectors to the start of the boot
  58. partition, for PC/AT systems.
  59. BootEntry - Supplies an optional pointer to the boot entry, for EFI systems.
  60. VolumeHandle - Supplies a pointer where a handle to the open volume will
  61. be returned on success.
  62. Return Value:
  63. Status code.
  64. --*/
  65. {
  66. HANDLE DiskHandle;
  67. KSTATUS Status;
  68. *VolumeHandle = NULL;
  69. DiskHandle = NULL;
  70. Status = FwOpenBootDisk(BootDriveNumber,
  71. PartitionOffset,
  72. BootEntry,
  73. &DiskHandle);
  74. if (!KSUCCESS(Status)) {
  75. goto OpenBootVolumeEnd;
  76. }
  77. Status = BopOpenVolume(DiskHandle, VolumeHandle);
  78. if (!KSUCCESS(Status)) {
  79. goto OpenBootVolumeEnd;
  80. }
  81. OpenBootVolumeEnd:
  82. if (!KSUCCESS(Status)) {
  83. if (DiskHandle != NULL) {
  84. FwCloseDisk(DiskHandle);
  85. }
  86. }
  87. return Status;
  88. }
  89. KSTATUS
  90. BoCloseVolume (
  91. PBOOT_VOLUME VolumeHandle
  92. )
  93. /*++
  94. Routine Description:
  95. This routine closes a disk handle.
  96. Arguments:
  97. VolumeHandle - Supplies the volume handle returned when the volume was
  98. opened.
  99. Return Value:
  100. Status code.
  101. --*/
  102. {
  103. KSTATUS Status;
  104. Status = FatUnmount(VolumeHandle->FileSystemHandle);
  105. if (!KSUCCESS(Status)) {
  106. return Status;
  107. }
  108. FwCloseDisk(VolumeHandle->DiskHandle);
  109. BoFreeMemory(VolumeHandle);
  110. return STATUS_SUCCESS;
  111. }
  112. KSTATUS
  113. BoOpenVolume (
  114. UCHAR PartitionId[FIRMWARE_PARTITION_ID_SIZE],
  115. PBOOT_VOLUME *Volume
  116. )
  117. /*++
  118. Routine Description:
  119. This routine closes a disk handle.
  120. Arguments:
  121. PartitionId - Supplies the ID of the partition to open.
  122. Volume - Supplies a pointer where a handle to the open volume will
  123. be returned on success.
  124. Return Value:
  125. Status code.
  126. --*/
  127. {
  128. HANDLE DiskHandle;
  129. KSTATUS Status;
  130. *Volume = NULL;
  131. DiskHandle = NULL;
  132. Status = FwOpenPartition(PartitionId, &DiskHandle);
  133. if (!KSUCCESS(Status)) {
  134. goto OpenVolumeEnd;
  135. }
  136. Status = BopOpenVolume(DiskHandle, Volume);
  137. if (!KSUCCESS(Status)) {
  138. goto OpenVolumeEnd;
  139. }
  140. OpenVolumeEnd:
  141. if (!KSUCCESS(Status)) {
  142. if (DiskHandle != NULL) {
  143. FwCloseDisk(DiskHandle);
  144. }
  145. }
  146. return Status;
  147. }
  148. KSTATUS
  149. BoLookupPath (
  150. PBOOT_VOLUME Volume,
  151. PFILE_ID StartingDirectory,
  152. PCSTR Path,
  153. PFILE_PROPERTIES FileProperties
  154. )
  155. /*++
  156. Routine Description:
  157. This routine attempts to look up the given file path.
  158. Arguments:
  159. Volume - Supplies a pointer to the volume token.
  160. StartingDirectory - Supplies an optional pointer to a file ID containing
  161. the directory to start path traversal from. If NULL, path lookup will
  162. start with the root of the volume.
  163. Path - Supplies a pointer to the path string to look up.
  164. FileProperties - Supplies a pointer where the properties for the file will
  165. be returned on success.
  166. Return Value:
  167. STATUS_SUCCESS on success.
  168. STATUS_PATH_NOT_FOUND if the given file path does not exist.
  169. Other error codes on other failures.
  170. --*/
  171. {
  172. ULONG ComponentSize;
  173. PSTR CurrentComponent;
  174. FILE_ID DirectoryId;
  175. PSTR NextComponent;
  176. PSTR PathCopy;
  177. ULONG PathLength;
  178. KSTATUS Status;
  179. PathCopy = NULL;
  180. PathLength = RtlStringLength(Path);
  181. if (PathLength == 0) {
  182. Status = STATUS_INVALID_PARAMETER;
  183. goto LookupPathEnd;
  184. }
  185. PathLength += 1;
  186. PathCopy = BoAllocateMemory(PathLength);
  187. if (PathCopy == NULL) {
  188. Status = STATUS_INSUFFICIENT_RESOURCES;
  189. goto LookupPathEnd;
  190. }
  191. RtlCopyMemory(PathCopy, Path, PathLength);
  192. //
  193. // Start with the root directory.
  194. //
  195. if (StartingDirectory != NULL) {
  196. DirectoryId = *StartingDirectory;
  197. RtlZeroMemory(FileProperties, sizeof(FILE_PROPERTIES));
  198. } else {
  199. Status = FatLookup(Volume->FileSystemHandle,
  200. TRUE,
  201. 0,
  202. NULL,
  203. 0,
  204. FileProperties);
  205. if (!KSUCCESS(Status)) {
  206. goto LookupPathEnd;
  207. }
  208. DirectoryId = FileProperties->FileId;
  209. }
  210. //
  211. // Remove any leading slashes.
  212. //
  213. CurrentComponent = PathCopy;
  214. while (*CurrentComponent == '/') {
  215. CurrentComponent += 1;
  216. PathLength -= 1;
  217. }
  218. //
  219. // Loop looking up directory entries.
  220. //
  221. while (TRUE) {
  222. NextComponent = RtlStringFindCharacter(CurrentComponent,
  223. '/',
  224. PathLength);
  225. if (NextComponent != NULL) {
  226. *NextComponent = '\0';
  227. NextComponent += 1;
  228. ComponentSize = (UINTN)NextComponent - (UINTN)CurrentComponent;
  229. } else {
  230. ComponentSize = PathLength;
  231. }
  232. if (ComponentSize > 1) {
  233. Status = FatLookup(Volume->FileSystemHandle,
  234. FALSE,
  235. DirectoryId,
  236. CurrentComponent,
  237. ComponentSize,
  238. FileProperties);
  239. if (!KSUCCESS(Status)) {
  240. goto LookupPathEnd;
  241. }
  242. }
  243. if (NextComponent == NULL) {
  244. break;
  245. }
  246. DirectoryId = FileProperties->FileId;
  247. PathLength -= ComponentSize;
  248. CurrentComponent = NextComponent;
  249. }
  250. LookupPathEnd:
  251. if (PathCopy != NULL) {
  252. BoFreeMemory(PathCopy);
  253. }
  254. return Status;
  255. }
  256. KSTATUS
  257. BoLoadFile (
  258. PBOOT_VOLUME Volume,
  259. PFILE_ID Directory,
  260. PSTR FileName,
  261. PVOID *FilePhysical,
  262. PUINTN FileSize,
  263. PULONGLONG ModificationDate
  264. )
  265. /*++
  266. Routine Description:
  267. This routine loads a file from disk into memory.
  268. Arguments:
  269. Volume - Supplies a pointer to the mounted volume to read the file from.
  270. Directory - Supplies an optional pointer to the ID of the directory to
  271. start path traversal from. If NULL, the root of the volume will be used.
  272. FileName - Supplies the name of the file to load.
  273. FilePhysical - Supplies a pointer where the file buffer's physical address
  274. will be returned. This routine will allocate the buffer to hold the
  275. file. If this parameter is NULL, the status code will reflect whether
  276. or not the file could be opened, but the file contents will not be
  277. loaded.
  278. FileSize - Supplies a pointer where the size of the file in bytes will be
  279. returned.
  280. ModificationDate - Supplies an optional pointer where the modification
  281. date of the file will be returned on success.
  282. Return Value:
  283. Status code.
  284. --*/
  285. {
  286. UINTN AlignedSize;
  287. UINTN BytesRead;
  288. FAT_SEEK_INFORMATION FatSeekInformation;
  289. PVOID File;
  290. FILE_PROPERTIES FileProperties;
  291. PFAT_IO_BUFFER IoBuffer;
  292. ULONGLONG LocalFileSize;
  293. ULONG PageSize;
  294. PVOID PhysicalBuffer;
  295. KSTATUS Status;
  296. File = NULL;
  297. IoBuffer = NULL;
  298. LocalFileSize = 0;
  299. PhysicalBuffer = NULL;
  300. PageSize = MmPageSize();
  301. Status = BoLookupPath(Volume, Directory, FileName, &FileProperties);
  302. if (!KSUCCESS(Status)) {
  303. goto LoadFileEnd;
  304. }
  305. Status = FatOpenFileId(Volume->FileSystemHandle,
  306. FileProperties.FileId,
  307. IO_ACCESS_READ,
  308. 0,
  309. &File);
  310. if (!KSUCCESS(Status)) {
  311. goto LoadFileEnd;
  312. }
  313. LocalFileSize = FileProperties.Size;
  314. ASSERT((UINTN)LocalFileSize == LocalFileSize);
  315. //
  316. // If the caller doesn't actually want the data, the work is done.
  317. //
  318. if (FilePhysical == NULL) {
  319. Status = STATUS_SUCCESS;
  320. goto LoadFileEnd;
  321. }
  322. //
  323. // Round the file size up to the nearest page.
  324. //
  325. AlignedSize = ALIGN_RANGE_UP(LocalFileSize + 1, PageSize);
  326. PhysicalBuffer = BoAllocateMemory(AlignedSize);
  327. if (PhysicalBuffer == NULL) {
  328. Status = STATUS_INSUFFICIENT_RESOURCES;
  329. goto LoadFileEnd;
  330. }
  331. IoBuffer = FatCreateIoBuffer(PhysicalBuffer, AlignedSize);
  332. if (IoBuffer == NULL) {
  333. Status = STATUS_INSUFFICIENT_RESOURCES;
  334. goto LoadFileEnd;
  335. }
  336. RtlZeroMemory(&FatSeekInformation, sizeof(FAT_SEEK_INFORMATION));
  337. Status = FatReadFile(File,
  338. &FatSeekInformation,
  339. IoBuffer,
  340. (UINTN)LocalFileSize,
  341. 0,
  342. NULL,
  343. &BytesRead);
  344. if (!KSUCCESS(Status)) {
  345. goto LoadFileEnd;
  346. }
  347. if (BytesRead != LocalFileSize) {
  348. Status = STATUS_FILE_CORRUPT;
  349. goto LoadFileEnd;
  350. }
  351. //
  352. // NULL terminate the file just in case someone tries to read off the end
  353. // of it.
  354. //
  355. *((PUCHAR)PhysicalBuffer + BytesRead) = '\0';
  356. LoadFileEnd:
  357. if (FilePhysical != NULL) {
  358. *FilePhysical = PhysicalBuffer;
  359. }
  360. if (FileSize != NULL) {
  361. *FileSize = LocalFileSize;
  362. }
  363. if (ModificationDate != NULL) {
  364. *ModificationDate = FileProperties.ModifiedTime.Seconds;
  365. }
  366. if (IoBuffer != NULL) {
  367. FatFreeIoBuffer(IoBuffer);
  368. }
  369. if (File != NULL) {
  370. FatCloseFile(File);
  371. }
  372. return Status;
  373. }
  374. KSTATUS
  375. BoStoreFile (
  376. PBOOT_VOLUME Volume,
  377. FILE_ID Directory,
  378. PSTR FileName,
  379. ULONG FileNameLength,
  380. PVOID FilePhysical,
  381. UINTN FileSize,
  382. ULONGLONG ModificationDate
  383. )
  384. /*++
  385. Routine Description:
  386. This routine stores a file buffer to disk.
  387. Arguments:
  388. Volume - Supplies a pointer to the mounted volume to read the file from.
  389. Directory - Supplies the file ID of the directory the file resides in.
  390. FileName - Supplies the name of the file to store.
  391. FileNameLength - Supplies the length of the file name buffer in bytes,
  392. including the null terminator.
  393. FilePhysical - Supplies a pointer to the buffer containing the file
  394. contents.
  395. FileSize - Supplies the size of the file buffer in bytes. The file will be
  396. truncated to this size if it previously existed and was larger.
  397. ModificationDate - Supplies the modification date to set.
  398. Return Value:
  399. Status code.
  400. --*/
  401. {
  402. UINTN BytesWritten;
  403. ULONG DesiredAccess;
  404. FILE_PROPERTIES DirectoryProperties;
  405. ULONGLONG DirectorySize;
  406. FAT_SEEK_INFORMATION FatSeekInformation;
  407. PVOID File;
  408. FILE_PROPERTIES FileProperties;
  409. PFAT_IO_BUFFER IoBuffer;
  410. ULONGLONG NewDirectorySize;
  411. KSTATUS Status;
  412. File = NULL;
  413. IoBuffer = NULL;
  414. RtlZeroMemory(&FileProperties, sizeof(FILE_PROPERTIES));
  415. FileProperties.Size = 0;
  416. ASSERT(Directory != 0);
  417. //
  418. // Load the file into memory.
  419. //
  420. DesiredAccess = IO_ACCESS_WRITE;
  421. Status = FatLookup(Volume->FileSystemHandle,
  422. FALSE,
  423. Directory,
  424. FileName,
  425. FileNameLength,
  426. &FileProperties);
  427. if (KSUCCESS(Status)) {
  428. Status = FatDeleteFileBlocks(Volume->FileSystemHandle,
  429. NULL,
  430. FileProperties.FileId,
  431. 0,
  432. TRUE);
  433. if (!KSUCCESS(Status)) {
  434. goto StoreFileEnd;
  435. }
  436. //
  437. // The file did not exist before. Create it.
  438. //
  439. } else if (Status == STATUS_PATH_NOT_FOUND) {
  440. //
  441. // Look up the directory.
  442. //
  443. RtlZeroMemory(&DirectoryProperties, sizeof(FILE_PROPERTIES));
  444. Status = FatLookup(Volume->FileSystemHandle,
  445. FALSE,
  446. Directory,
  447. ".",
  448. sizeof("."),
  449. &DirectoryProperties);
  450. if (!KSUCCESS(Status)) {
  451. goto StoreFileEnd;
  452. }
  453. FileProperties.Type = IoObjectRegularFile;
  454. FileProperties.Permissions = FILE_PERMISSION_USER_READ |
  455. FILE_PERMISSION_USER_WRITE |
  456. FILE_PERMISSION_GROUP_READ |
  457. FILE_PERMISSION_GROUP_WRITE |
  458. FILE_PERMISSION_OTHER_READ;
  459. FatGetCurrentSystemTime(&(FileProperties.StatusChangeTime));
  460. Status = FatCreate(Volume->FileSystemHandle,
  461. Directory,
  462. FileName,
  463. FileNameLength,
  464. &NewDirectorySize,
  465. &FileProperties);
  466. DirectorySize = DirectoryProperties.Size;
  467. if (NewDirectorySize > DirectorySize) {
  468. DirectoryProperties.Size = NewDirectorySize;
  469. Status = FatWriteFileProperties(Volume->FileSystemHandle,
  470. &DirectoryProperties,
  471. 0);
  472. if (!KSUCCESS(Status)) {
  473. goto StoreFileEnd;
  474. }
  475. }
  476. //
  477. // Some other error occurred, bail out.
  478. //
  479. } else if (!KSUCCESS(Status)) {
  480. goto StoreFileEnd;
  481. }
  482. //
  483. // Open up the now-empty file.
  484. //
  485. Status = FatOpenFileId(Volume->FileSystemHandle,
  486. FileProperties.FileId,
  487. DesiredAccess,
  488. 0,
  489. &File);
  490. if (!KSUCCESS(Status)) {
  491. goto StoreFileEnd;
  492. }
  493. //
  494. // Create an I/O buffer and write the data out.
  495. //
  496. IoBuffer = FatCreateIoBuffer(FilePhysical, FileSize);
  497. if (IoBuffer == NULL) {
  498. Status = STATUS_INSUFFICIENT_RESOURCES;
  499. goto StoreFileEnd;
  500. }
  501. RtlZeroMemory(&FatSeekInformation, sizeof(FAT_SEEK_INFORMATION));
  502. Status = FatWriteFile(File,
  503. &FatSeekInformation,
  504. IoBuffer,
  505. FileSize,
  506. 0,
  507. NULL,
  508. &BytesWritten);
  509. if (!KSUCCESS(Status)) {
  510. goto StoreFileEnd;
  511. }
  512. if (BytesWritten != FileSize) {
  513. Status = STATUS_FILE_CORRUPT;
  514. goto StoreFileEnd;
  515. }
  516. //
  517. // Update the metadata.
  518. //
  519. FileProperties.Size = FileSize;
  520. FileProperties.ModifiedTime.Seconds = ModificationDate;
  521. FileProperties.AccessTime.Seconds = ModificationDate;
  522. Status = FatWriteFileProperties(Volume->FileSystemHandle,
  523. &FileProperties,
  524. 0);
  525. if (!KSUCCESS(Status)) {
  526. goto StoreFileEnd;
  527. }
  528. StoreFileEnd:
  529. if (IoBuffer != NULL) {
  530. FatFreeIoBuffer(IoBuffer);
  531. }
  532. if (File != NULL) {
  533. FatCloseFile(File);
  534. }
  535. return Status;
  536. }
  537. //
  538. // --------------------------------------------------------- Internal Functions
  539. //
  540. KSTATUS
  541. BopOpenVolume (
  542. HANDLE DiskHandle,
  543. PBOOT_VOLUME *VolumeHandle
  544. )
  545. /*++
  546. Routine Description:
  547. This routine mounts a volume on an open disk handle and creates a volume
  548. handle representing that connection.
  549. Arguments:
  550. DiskHandle - Supplies the open disk or partition handle from the firmware.
  551. PartitionOffset - Supplies the offset in sectors to the start of the
  552. partition.
  553. VolumeHandle - Supplies a pointer where a handle to the open volume will
  554. be returned on success.
  555. Return Value:
  556. Status code.
  557. --*/
  558. {
  559. PBOOT_VOLUME BootVolume;
  560. KSTATUS Status;
  561. BootVolume = BoAllocateMemory(sizeof(BOOT_VOLUME));
  562. if (BootVolume == NULL) {
  563. Status = STATUS_INSUFFICIENT_RESOURCES;
  564. goto OpenVolumeEnd;
  565. }
  566. RtlZeroMemory(BootVolume, sizeof(BOOT_VOLUME));
  567. BootVolume->DiskHandle = DiskHandle;
  568. //
  569. // Attempt to mount the device.
  570. //
  571. BootVolume->Parameters.DeviceToken = BootVolume;
  572. BootVolume->Parameters.BlockSize = FwGetDiskSectorSize(DiskHandle);
  573. BootVolume->Parameters.BlockCount = FwGetDiskSectorCount(DiskHandle);
  574. Status = FatMount(&(BootVolume->Parameters),
  575. 0,
  576. &(BootVolume->FileSystemHandle));
  577. if (!KSUCCESS(Status)) {
  578. goto OpenVolumeEnd;
  579. }
  580. OpenVolumeEnd:
  581. if (!KSUCCESS(Status)) {
  582. if (BootVolume != NULL) {
  583. ASSERT(BootVolume->FileSystemHandle == NULL);
  584. BoFreeMemory(BootVolume);
  585. BootVolume = NULL;
  586. }
  587. }
  588. *VolumeHandle = BootVolume;
  589. return Status;
  590. }