Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2011. All rights reserved.      ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. ; =============================================================================
  9. ; ================================= Constants =================================
  10. ; =============================================================================
  11. ; Error codes for callback functions.
  12. DISK_STATUS_OK              = 0 ; success
  13. DISK_STATUS_GENERAL_ERROR   = -1; if no other code is suitable
  14. DISK_STATUS_INVALID_CALL    = 1 ; invalid input parameters
  15. DISK_STATUS_NO_MEDIA        = 2 ; no media present
  16. DISK_STATUS_END_OF_MEDIA    = 3 ; end of media while reading/writing data
  17. ; Driver flags. Represent bits in DISK.DriverFlags.
  18. DISK_NO_INSERT_NOTIFICATION = 1
  19. ; Media flags. Represent bits in DISKMEDIAINFO.Flags.
  20. DISK_MEDIA_READONLY = 1
  21.  
  22. ; If we see too many partitions, probably there is some error on the disk.
  23. ; 256 partitions should be enough for any reasonable use.
  24. ; Also, the same number is limiting the number of MBRs to process; if we see
  25. ; too many MBRs, probably there is a loop in the MBR structure.
  26. MAX_NUM_PARTITIONS = 256
  27.  
  28. ; =============================================================================
  29. ; ================================ Structures =================================
  30. ; =============================================================================
  31. ; This structure defines all callback functions for working with the physical
  32. ; device. They are implemented by a driver. Objects with this structure reside
  33. ; in a driver.
  34. struct DISKFUNC
  35. .strucsize      dd      ?
  36. ; Size of the structure. This field is intended for possible extensions of
  37. ; this structure. If a new function is added to this structure and a driver
  38. ; implements an old version, the caller can detect this by checking .strucsize,
  39. ; so the driver remains compatible.
  40. .close          dd      ?
  41. ; The pointer to the function which frees all driver-specific resources for
  42. ; the disk.
  43. ; Optional, may be NULL.
  44. ; void close(void* userdata);
  45. .closemedia     dd      ?
  46. ; The pointer to the function which informs the driver that the kernel has
  47. ; finished all processing with the current media. If media is removed, the
  48. ; driver should decline all requests to that media with DISK_STATUS_NO_MEDIA,
  49. ; even if new media is inserted, until this function is called. If media is
  50. ; removed, a new call to 'disk_media_changed' is not allowed until this
  51. ; function is called.
  52. ; Optional, may be NULL (if media is not removable).
  53. ; void closemedia(void* userdata);
  54. .querymedia     dd      ?
  55. ; The pointer to the function which determines capabilities of the media.
  56. ; int querymedia(void* userdata, DISKMEDIAINFO* info);
  57. ; Return value: one of DISK_STATUS_*
  58. .read           dd      ?
  59. ; The pointer to the function which reads data from the device.
  60. ; int read(void* userdata, void* buffer, __int64 startsector, int* numsectors);
  61. ; input: *numsectors = number of sectors to read
  62. ; output: *numsectors = number of sectors which were successfully read
  63. ; Return value: one of DISK_STATUS_*
  64. .write          dd      ?
  65. ; The pointer to the function which writes data to the device.
  66. ; Optional, may be NULL.
  67. ; int write(void* userdata, void* buffer, __int64 startsector, int* numsectors);
  68. ; input: *numsectors = number of sectors to write
  69. ; output: *numsectors = number of sectors which were successfully written
  70. ; Return value: one of DISK_STATUS_*
  71. .flush          dd      ?
  72. ; The pointer to the function which flushes the internal device cache.
  73. ; Optional, may be NULL.
  74. ; int flush(void* userdata);
  75. ; Return value: one of DISK_STATUS_*
  76. ; Note that read/write are called by the cache manager, so a driver should not
  77. ; create a software cache. This function is implemented for flushing a hardware
  78. ; cache, if it exists.
  79. ends
  80.  
  81. ; This structure holds an information about a media.
  82. ; Objects with this structure are allocated by the kernel as a part of DISK
  83. ; structure and filled by a driver in the 'querymedia' callback.
  84. struct DISKMEDIAINFO
  85. .Flags          dd      ?
  86. ; Combination of DISK_MEDIA_* bits.
  87. .SectorSize     dd      ?
  88. ; Size of the sector.
  89. .Capacity       dq      ?
  90. ; Size of the media in sectors.
  91. ends
  92.  
  93. ; This structure represents a disk device and its media for the kernel.
  94. ; This structure is allocated by the kernel in the 'disk_add' function,
  95. ; freed in the 'disk_dereference' function.
  96. struct DISK
  97. ; Fields of disk object
  98. .Next           dd      ?
  99. .Prev           dd      ?
  100. ; All disk devices are linked in one list with these two fields.
  101. ; Head of the list is the 'disk_list' variable.
  102. .Functions      dd      ?
  103. ; Pointer to the 'DISKFUNC' structure with driver functions.
  104. .Name           dd      ?
  105. ; Pointer to the string used for accesses through the global filesystem.
  106. .UserData       dd      ?
  107. ; This field is passed to all callback functions so a driver can decide which
  108. ; physical device is addressed.
  109. .DriverFlags    dd      ?
  110. ; Bitfield. Currently only DISK_NO_INSERT_NOTIFICATION bit is defined.
  111. ; If it is set, the driver will never issue 'disk_media_changed' notification
  112. ; with argument set to true, so the kernel must try to detect media during
  113. ; requests from the file system.
  114. .RefCount       dd      ?
  115. ; Count of active references to this structure. One reference is kept during
  116. ; the lifetime of the structure between 'disk_add' and 'disk_del'.
  117. ; Another reference is taken during any filesystem operation for this disk.
  118. ; One reference is added if media is inserted.
  119. ; The structure is destroyed when the reference count decrements to zero:
  120. ; this usually occurs in 'disk_del', but can be delayed to the end of last
  121. ; filesystem operation, if one is active.
  122. .MediaLock              dd      ?
  123. ; Lock to protect the MEDIA structure. See the description after
  124. ; 'disk_list_mutex' for the locking strategy.
  125. ; Fields of media object
  126. .MediaInserted          db      ?
  127. ; 0 if media is not inserted, nonzero otherwise.
  128. .MediaUsed              db      ?
  129. ; 0 if media fields are not used, nonzero otherwise. If .MediaRefCount is
  130. ; nonzero, this field is nonzero too; however, when .MediaRefCount goes
  131. ; to zero, there is some time interval during which media object is still used.
  132.                 align 4
  133. ; The following fields are not valid unless either .MediaInserted is nonzero
  134. ; or they are accessed from a code which has obtained the reference when
  135. ; .MediaInserted was nonzero.
  136. .MediaRefCount          dd      ?
  137. ; Count of active references to the media object. One reference is kept during
  138. ; the lifetime of the media between two calls to 'disk_media_changed'.
  139. ; Another reference is taken during any filesystem operation for this media.
  140. ; The callback 'closemedia' is called when the reference count decrements to
  141. ; zero: this usually occurs in 'disk_media_changed', but can be delayed to the
  142. ; end of last filesystem operation, if one is active.
  143. .MediaInfo              DISKMEDIAINFO
  144. ; This field keeps an information about the current media.
  145. .NumPartitions  dd      ?
  146. ; Number of partitions on this media.
  147. .Partitions     dd      ?
  148. ; Pointer to array of .NumPartitions pointers to PARTITION structures.
  149. ends
  150.  
  151. ; This structure represents one partition for the kernel. This is a base
  152. ; template, the actual contents after common fields is determined by the
  153. ; file system code for this partition.
  154. struct PARTITION
  155. .FirstSector    dq      ?
  156. ; First sector of the partition.
  157. .Length         dq      ?
  158. ; Length of the partition in sectors.
  159. .FSUserFunctions        dd      ?
  160. ; Handlers for the sysfunction 70h. This field is a pointer to the following
  161. ; array. The first dword is a number of supported subfunctions, other dwords
  162. ; point to handlers of corresponding subfunctions.
  163. ; This field is 0 if file system is not recognized.
  164. ; ...fs-specific data may follow...
  165. ends
  166.  
  167. ; This is an external structure, it represents an entry in the partition table.
  168. struct PARTITION_TABLE_ENTRY
  169. .Bootable       db      ?
  170. ; 80h = bootable partition, 0 = non-bootable partition, other values = invalid
  171. .FirstHead      db      ?
  172. .FirstSector    db      ?
  173. .FirstTrack     db      ?
  174. ; Coordinates of first sector in CHS.
  175. .Type           db      ?
  176. ; Partition type, one of predefined constants. 0 = empty, several types denote
  177. ; extended partition (see process_partition_table_entry), we are not interested
  178. ; in other values.
  179. .LastHead       db      ?
  180. .LastSector     db      ?
  181. .LastTrack      db      ?
  182. ; Coordinates of last sector in CHS.
  183. .FirstAbsSector dd      ?
  184. ; Coordinate of first sector in LBA.
  185. .Length         dd      ?
  186. ; Length of the partition in sectors.
  187. ends
  188.  
  189. ; =============================================================================
  190. ; ================================ Global data ================================
  191. ; =============================================================================
  192. iglobal
  193. ; The pseudo-item for the list of all DISK structures.
  194. ; Initialized to the empty list.
  195. disk_list:
  196.         dd      disk_list
  197.         dd      disk_list
  198. endg
  199. uglobal
  200. ; This mutex guards all operations with the global list of DISK structures.
  201. disk_list_mutex dd      0
  202. ; * There are two dependent objects, a disk and a media. In the simplest case
  203. ;   disk and media are both non-removable. However, in the general case both
  204. ;   can be removed at any time, simultaneously or only media, this makes things
  205. ;   complicated.
  206. ; * For efficiency, both disk and media objects are located in the one
  207. ;   structure named DISK. However, logically they are different.
  208. ; * The following operations use data of disk object: adding (disk_add);
  209. ;   deleting (disk_del); filesystem (fs_lfn which eventually calls
  210. ;   dyndisk_handler or dyndisk_enum_root).
  211. ; * The following operations use data of media object: adding/removing
  212. ;   (disk_media_changed); filesystem (fs_lfn which eventually calls
  213. ;   dyndisk_handler; dyndisk_enum_root doesn't work with media).
  214. ; * Notifications disk_add, disk_media_changed, disk_del are synchronized
  215. ;   between themselves, this is a requirement for the driver. However, file
  216. ;   system operations are asynchronous, can be issued at any time by any
  217. ;   thread.
  218. ; * We must prevent a situation when a filesystem operation thinks that the
  219. ;   object is still valid but in fact the notification has destroyed the
  220. ;   object. So we keep a reference counter for both disk and media and destroy
  221. ;   the object when this counter goes to zero.
  222. ; * The driver must know when it is safe to free driver-allocated resources.
  223. ;   The object can be alive even after death notification has completed.
  224. ;   We use special callbacks to satisfy both assertions: 'close' for the disk
  225. ;   and 'closemedia' for the media. The destruction of the object includes
  226. ;   calling the corresponding callback.
  227. ; * Each filesystem operation keeps one reference for the disk and one
  228. ;   reference for the media. Notification disk_del forces notification on the
  229. ;   media death, so the reference counter for the disk is always not less than
  230. ;   the reference counter for the media.
  231. ; * Two operations "get the object" and "increment the reference counter" can
  232. ;   not be done simultaneously. We use a mutex to guard the consistency here.
  233. ;   It must be a part of the container for the object, so that this mutex can
  234. ;   be acquired as a part of getting the object from the container. The
  235. ;   container for disk object is the global list, and this list is guarded by
  236. ;   'disk_list_mutex'. The container for media object is the disk object, and
  237. ;   the corresponding mutex is DISK.MediaLock.
  238. ; * Notifications do not change the data of objects, they can only remove
  239. ;   objects. Thus we don't need another synchronization at this level. If two
  240. ;   filesystem operations are referencing the same filesystem data, this is
  241. ;   better resolved at the level of the filesystem.
  242. endg
  243.  
  244. iglobal
  245. ; The function 'disk_scan_partitions' needs two 512-byte buffers for
  246. ; MBR and bootsectors data. It can not use the static buffers always,
  247. ; since it can be called for two or more disks in parallel. However, this
  248. ; case is not typical. We reserve two static 512-byte buffers and a flag
  249. ; that these buffers are currently used. If 'disk_scan_partitions' detects that
  250. ; the buffers are currently used, it allocates buffers from the heap.
  251. ; The flag is implemented as a global dword variable. When the static buffers
  252. ; are not used, the value is -1. When the static buffers are used, the value
  253. ; is normally 0 and temporarily can become greater. The function increments
  254. ; this value. If the resulting value is zero, it uses the buffers and
  255. ; decrements the value when the job is done. Otherwise, it immediately
  256. ; decrements the value and uses buffers from the heap, allocated in the
  257. ; beginning and freed in the end.
  258. partition_buffer_users  dd      -1
  259. endg
  260. uglobal
  261. ; The static buffers for MBR and bootsectors data.
  262. align 16
  263. mbr_buffer      rb      512
  264. bootsect_buffer rb      512
  265. endg
  266.  
  267. iglobal
  268. ; This is the array of default implementations of driver callbacks.
  269. ; Same as DRIVERFUNC structure except for the first field; all functions must
  270. ; have the default implementations.
  271. align 4
  272. disk_default_callbacks:
  273.         dd      disk_default_close
  274.         dd      disk_default_closemedia
  275.         dd      disk_default_querymedia
  276.         dd      disk_default_read
  277.         dd      disk_default_write
  278.         dd      disk_default_flush
  279. endg
  280.  
  281. ; =============================================================================
  282. ; ================================= Functions =================================
  283. ; =============================================================================
  284.  
  285. ; This function registers a disk device.
  286. ; This includes:
  287. ; - allocating an internal structure describing this device;
  288. ; - registering this structure in the global filesystem.
  289. ; The function initializes the disk as if there is no media. If a media is
  290. ; present, the function 'disk_media_changed' should be called after this
  291. ; function succeeds.
  292. ; Parameters:
  293. ; [esp+4] = pointer to DISKFUNC structure with the callbacks
  294. ; [esp+8] = pointer to name (ASCIIZ string)
  295. ; [esp+12] = userdata to be passed to the callbacks as is.
  296. ; [esp+16] = flags, bitfield. Currently only DISK_NO_INSERT_NOTIFICATION bit
  297. ;            is defined.
  298. ; Return value:
  299. ; NULL = operation has failed
  300. ; non-NULL = handle of the disk. This handle can be used
  301. ; in the operations with other Disk* functions.
  302. ; The handle is the pointer to the internal structure DISK.
  303. disk_add:
  304.         push    ebx esi         ; save used registers to be stdcall
  305. ; 1. Allocate the DISK structure.
  306. ; 1a. Call the heap manager.
  307.         push    sizeof.DISK
  308.         pop     eax
  309.         call    malloc
  310. ; 1b. Check the result. If allocation failed, return (go to 9) with eax = 0.
  311.         test    eax, eax
  312.         jz      .nothing
  313. ; 2. Copy disk name to the DISK structure.
  314. ; 2a. Get length of the name, including the terminating zero.
  315.         mov     esi, [esp+8+8]  ; esi = pointer to name
  316.         push    eax             ; save allocated pointer to DISK
  317.         xor     eax, eax        ; the argument of malloc() is in eax
  318. @@:
  319.         inc     eax
  320.         cmp     byte [esi+eax-1], 0
  321.         jnz     @b
  322. ; 2b. Call the heap manager.
  323.         call    malloc
  324. ; 2c. Check the result. If allocation failed, go to 7.
  325.         pop     ebx             ; restore allocated pointer to DISK
  326.         test    eax, eax
  327.         jz      .free
  328. ; 2d. Store the allocated pointer to the DISK structure.
  329.         mov     [ebx+DISK.Name], eax
  330. ; 2e. Copy the name.
  331. @@:
  332.         mov     dl, [esi]
  333.         mov     [eax], dl
  334.         inc     esi
  335.         inc     eax
  336.         test    dl, dl
  337.         jnz     @b
  338. ; 3. Copy other arguments of the function to the DISK structure.
  339.         mov     eax, [esp+4+8]
  340.         mov     [ebx+DISK.Functions], eax
  341.         mov     eax, [esp+12+8]
  342.         mov     [ebx+DISK.UserData], eax
  343.         mov     eax, [esp+16+8]
  344.         mov     [ebx+DISK.DriverFlags], eax
  345. ; 4. Initialize other fields of the DISK structure.
  346. ; Media is not inserted, initialized state of mutex is zero,
  347. ; reference counter is 1.
  348.         xor     eax, eax
  349.         mov     dword [ebx+DISK.MediaInserted], eax
  350.         mov     [ebx+DISK.MediaLock], eax
  351.         inc     eax
  352.         mov     [ebx+DISK.RefCount], eax
  353. ; The DISK structure is initialized.
  354. ; 5. Insert the new structure to the global list.
  355.         xchg    eax, ebx        ; now eax = pointer to DISK
  356. ; 5a. Acquire the mutex.
  357.         mov     ebx, disk_list_mutex
  358.         call    wait_mutex
  359. ; 5b. Insert item to the tail of double-linked list.
  360.         mov     edx, disk_list
  361.         mov     ecx, [edx+DISK.Prev]
  362.         mov     [eax+DISK.Prev], ecx
  363.         mov     [eax+DISK.Next], edx
  364.         mov     [edx+DISK.Prev], eax
  365.         mov     [ecx+DISK.Next], eax
  366. ; 5c. Release the mutex.
  367.         mov     dword [ebx], 0
  368. ; 6. Return with eax = pointer to DISK.
  369.         jmp     .nothing
  370. .free:
  371. ; Memory allocation for DISK structure succeeded, but for disk name failed.
  372. ; 7. Free the DISK structure.
  373.         xchg    eax, ebx
  374.         call    free
  375. ; 8. Return with eax = 0.
  376.         xor     eax, eax
  377. .nothing:
  378. ; 9. Return.
  379.         pop     esi ebx         ; restore used registers to be stdcall
  380.         ret     16              ; purge 4 dword arguments to be stdcall
  381.  
  382. ; This function deletes a disk device from the global filesystem.
  383. ; This includes:
  384. ; - removing a media including all partitions;
  385. ; - deleting this structure from the global filesystem;
  386. ; - dereferencing the DISK structure and possibly destroying it.
  387. ; Parameters:
  388. ; [esp+4] = handle of the disk, i.e. the pointer to the DISK structure.
  389. ; Return value: none.
  390. disk_del:
  391.         push    ebx esi         ; save used registers to be stdcall
  392. ; 1. Force media to be removed. If the media is already removed, the
  393. ; call does nothing.
  394.         mov     esi, [esp+4+8]  ; esi = handle of the disk
  395.         stdcall disk_media_changed, esi, 0
  396. ; 2. Delete the structure from the global list.
  397. ; 2a. Acquire the mutex.
  398.         mov     ebx, disk_list_mutex
  399.         call    wait_mutex
  400. ; 2b. Delete item from double-linked list.
  401.         mov     eax, [esi+DISK.Next]
  402.         mov     edx, [esi+DISK.Prev]
  403.         mov     [eax+DISK.Prev], edx
  404.         mov     [edx+DISK.Next], eax
  405. ; 2c. Release the mutex.
  406.         mov     dword [ebx], 0
  407. ; 3. The structure still has one reference created in disk_add. Remove this
  408. ; reference. If there are no other references, disk_dereference will free the
  409. ; structure.
  410.         call    disk_dereference
  411. ; 4. Return.
  412.         pop     esi ebx         ; restore used registers to be stdcall
  413.         ret     4               ; purge 1 dword argument to be stdcall
  414.  
  415. ; This is an internal function which removes a previously obtained reference
  416. ; to the disk. If this is the last reference, this function lets the driver
  417. ; finalize all associated data, and afterwards frees the DISK structure.
  418. ; esi = pointer to DISK structure
  419. disk_dereference:
  420. ; 1. Decrement reference counter. Use atomic operation to correctly handle
  421. ; possible simultaneous calls.
  422. lock    dec     [esi+DISK.RefCount]
  423. ; 2. If the result is nonzero, there are other references, so nothing to do.
  424. ; In this case, return (go to 4).
  425.         jnz     .nothing
  426. ; 3. If we are here, we just removed the last reference and must destroy the
  427. ; disk object.
  428. ; 3a. Call the driver.
  429.         mov     al, DISKFUNC.close
  430.         stdcall disk_call_driver
  431. ; 3b. Free the structure.
  432.         xchg    eax, esi
  433.         call    free
  434. ; 4. Return.
  435. .nothing:
  436.         ret
  437.  
  438. ; This is an internal function which removes a previously obtained reference
  439. ; to the media. If this is the last reference, this function calls 'closemedia'
  440. ; callback to signal the driver that the processing has finished and it is safe
  441. ; to inform about a new media.
  442. ; esi = pointer to DISK structure
  443. disk_media_dereference:
  444. ; 1. Decrement reference counter. Use atomic operation to correctly handle
  445. ; possible simultaneous calls.
  446. lock    dec     [esi+DISK.MediaRefCount]
  447. ; 2. If the result is nonzero, there are other references, so nothing to do.
  448. ; In this case, return (go to 4).
  449.         jnz     .nothing
  450. ; 3. If we are here, we just removed the last reference and must destroy the
  451. ; media object.
  452. ; Note that the same place inside the DISK structure is reused for all media
  453. ; objects, so we must guarantee that reusing does not happen while freeing.
  454. ; Reusing is only possible when someone processes a new media. There are two
  455. ; mutually exclusive variants:
  456. ; * driver issues media insert notifications (DISK_NO_INSERT_NOTIFICATION bit
  457. ;   in DISK.DriverFlags is not set). In this case, we require from the driver
  458. ;   that such notification (except for the first one) can occur only after a
  459. ;   call to 'closemedia' callback.
  460. ; * driver does not issue media insert notifications. In this case, the kernel
  461. ;   itself must sometimes check whether media is inserted. We have the flag
  462. ;   DISK.MediaUsed, visible to the kernel. This flag signals to the other parts
  463. ;   of kernel that the way is free.
  464. ; In the first case other parts of the kernel do not use DISK.MediaUsed, so it
  465. ; does not matter when this flag is cleared. In the second case this flag must
  466. ; be cleared after all other actions, including call to 'closemedia'.
  467. ; 3a. Free all partitions.
  468.         push    esi edi
  469.         mov     edi, [esi+DISK.NumPartitions]
  470.         mov     esi, [esi+DISK.Partitions]
  471.         test    edi, edi
  472.         jz      .nofree
  473. .freeloop:
  474.         lodsd
  475.         call    free
  476.         dec     edi
  477.         jnz     .freeloop
  478. .nofree:
  479.         pop     edi esi
  480. ; 3b. Call the driver.
  481.         mov     al, DISKFUNC.closemedia
  482.         stdcall disk_call_driver
  483. ; 3c. Clear the flag.
  484.         mov     [esi+DISK.MediaUsed], 0
  485. .nothing:
  486.         ret
  487.  
  488. ; This function is called by the driver and informs the kernel that the media
  489. ; has changed. If the media is non-removable, it is called exactly once
  490. ; immediately after 'disk_add' and once from 'disk_del'.
  491. ; Parameters:
  492. ; [esp+4] = handle of the disk, i.e. the pointer to the DISK structure.
  493. ; [esp+8] = new status of the media: zero = no media, nonzero = media inserted.
  494. disk_media_changed:
  495.         push    ebx esi edi             ; save used registers to be stdcall
  496. ; 1. Remove the existing media, if it is present.
  497.         mov     esi, [esp+4+12]         ; esi = pointer to DISK
  498. ; 1a. Check whether it is present. Since DISK.MediaInserted is changed only
  499. ; in this function and calls to this function are synchronized, no lock is
  500. ; required for checking.
  501.         cmp     [esi+DISK.MediaInserted], 0
  502.         jz      .noremove
  503. ; We really need to remove the media.
  504. ; 1b. Acquire mutex.
  505.         lea     ebx, [esi+DISK.MediaLock]
  506.         call    wait_mutex
  507. ; 1c. Clear the flag.
  508.         mov     [esi+DISK.MediaInserted], 0
  509. ; 1d. Release mutex.
  510.         mov     dword [ebx], 0
  511. ; 1e. Remove the "lifetime" reference and possibly destroy the structure.
  512.         call    disk_media_dereference
  513. .noremove:
  514. ; 2. Test whether there is new media.
  515.         cmp     dword [esp+8+12], 0
  516.         jz      .noinsert
  517. ; Yep, there is.
  518. ; 3. Process the new media. We assume that all media fields are available to
  519. ; use, see comments in 'disk_media_dereference' (this covers using by previous
  520. ; media referencers) and note that calls to this function are synchronized
  521. ; (this covers using by new media referencers).
  522. ; 3a. Call the 'querymedia' callback.
  523. ; .Flags are set to zero for possible future extensions.
  524.         lea     edx, [esi+DISK.MediaInfo]
  525.         and     [edx+DISKMEDIAINFO.Flags], 0
  526.         mov     al, DISKFUNC.querymedia
  527.         stdcall disk_call_driver, edx
  528. ; 3b. Check the result of the callback. Abort if it failed.
  529.         test    eax, eax
  530.         jnz     .noinsert
  531. ; 3c. Acquire the lifetime reference for the media object.
  532.         inc     [esi+DISK.MediaRefCount]
  533. ; 3d. Scan for partitions. Ignore result; the list of partitions is valid even
  534. ; on errors.
  535.         call    disk_scan_partitions
  536. ; 3e. Media is inserted and available for use.
  537.         inc     [esi+DISK.MediaInserted]
  538. .noinsert:
  539. ; 4. Return.
  540.         pop     edi esi ebx             ; restore used registers to be stdcall
  541.         ret     8                       ; purge 2 dword arguments to be stdcall
  542.  
  543. ; This function is a thunk for all functions of a disk driver.
  544. ; It checks whether the referenced function is implemented in the driver.
  545. ; If so, this function jumps to the function in the driver.
  546. ; Otherwise, it jumps to the default implementation.
  547. ; al = offset of function in the DISKFUNC structure;
  548. ; esi = pointer to the DISK structure;
  549. ; stack is the same as for the corresponding function except that the
  550. ; first parameter (void* userdata) is prepended automatically.
  551. disk_call_driver:
  552.         movzx   eax, al ; eax = offset of function in the DISKFUNC structure
  553. ; 1. Prepend the first argument to the stack.
  554.         pop     ecx     ; ecx = return address
  555.         push    [esi+DISK.UserData]     ; add argument
  556.         push    ecx     ; save return address
  557. ; 2. Check that the required function is inside the table. If not, go to 5.
  558.         mov     ecx, [esi+DISK.Functions]
  559.         cmp     eax, [ecx+DISKFUNC.strucsize]
  560.         jae     .default
  561. ; 3. Check that the required function is implemented. If not, go to 5.
  562.         mov     ecx, [ecx+eax]
  563.         test    ecx, ecx
  564.         jz      .default
  565. ; 4. Jump to the required function.
  566.         jmp     ecx
  567. .default:
  568. ; 5. Driver does not implement the required function; use default implementation.
  569.         jmp     dword [disk_default_callbacks+eax-4]
  570.  
  571. ; The default implementation of DISKFUNC.querymedia.
  572. disk_default_querymedia:
  573.         push    DISK_STATUS_INVALID_CALL
  574.         pop     eax
  575.         ret     8
  576.  
  577. ; The default implementation of DISKFUNC.read and DISKFUNC.write.
  578. disk_default_read:
  579. disk_default_write:
  580.         push    DISK_STATUS_INVALID_CALL
  581.         pop     eax
  582.         ret     20
  583.  
  584. ; The default implementation of DISKFUNC.close, DISKFUNC.closemedia and
  585. ; DISKFUNC.flush.
  586. disk_default_close:
  587. disk_default_closemedia:
  588. disk_default_flush:
  589.         xor     eax, eax
  590.         ret     4
  591.  
  592. ; This is an internal function called from 'disk_media_changed' when new media
  593. ; is detected. It creates the list of partitions for the media.
  594. ; If media is not partitioned, then the list consists of one partition which
  595. ; covers all the media.
  596. ; esi = pointer to the DISK structure.
  597. disk_scan_partitions:
  598. ; 1. Initialize .NumPartitions and .Partitions fields as zeros: empty list.
  599.         and     [esi+DISK.NumPartitions], 0
  600.         and     [esi+DISK.Partitions], 0
  601. ; 2. Currently we can work only with 512-bytes sectors. Check this restriction.
  602. ; The only exception is 2048-bytes CD/DVD, but they are not supported yet by
  603. ; this code.
  604.         cmp     [esi+DISK.MediaInfo.SectorSize], 512
  605.         jz      .doscan
  606.         DEBUGF 1,'K : sector size is %d, only 512 is supported\n',[esi+DISK.MediaInfo.SectorSize]
  607.         ret
  608. .doscan:
  609. ; 3. Acquire the buffer for MBR and bootsector tests. See the comment before
  610. ; the 'partition_buffer_users' variable.
  611.         mov     ebx, mbr_buffer         ; assume the global buffer is free
  612. lock    inc     [partition_buffer_users]
  613.         jz      .buffer_acquired        ; yes, it is free
  614. lock    dec     [partition_buffer_users]        ; no, we must allocate
  615.         stdcall kernel_alloc, 1024
  616.         test    eax, eax
  617.         jz      .nothing
  618.         xchg    eax, ebx
  619. .buffer_acquired:
  620. ; MBR/EBRs are organized in the chain. We use a loop over MBR/EBRs, but no
  621. ; more than MAX_NUM_PARTITION times.
  622. ; 4. Prepare things for the loop.
  623. ; ebp will hold the sector number for current MBR/EBR.
  624. ; [esp] will hold the sector number for current extended partition, if there
  625. ; is one.
  626. ; [esp+4] will hold the counter that prevents long loops.
  627.         push    ebp             ; save ebp
  628.         push    MAX_NUM_PARTITIONS      ; the counter of max MBRs to process
  629.         xor     ebp, ebp        ; start from sector zero
  630.         push    ebp             ; no extended partition yet
  631. .new_mbr:
  632. ; 5. Read the current sector.
  633. ; Note that 'read' callback operates with 64-bit sector numbers, so we must
  634. ; push additional zero as a high dword of sector number.
  635.         mov     al, DISKFUNC.read
  636.         stdcall disk_call_driver, ebx, ebp, 0, 1
  637. ; 6. If the read has failed, abort the loop.
  638.         test    eax, eax
  639.         jz      .mbr_failed
  640. ; 7. Check the MBR/EBR signature. If it is wrong, abort the loop.
  641. ; Soon we will access the partition table which starts at ebx+0x1BE,
  642. ; so we can fill its address right now. If we do it now, then the addressing
  643. ; [ecx+0x40] is shorter than [ebx+0x1fe]: one-byte offset vs 4-bytes offset.
  644.         lea     ecx, [ebx+0x1be]        ; ecx -> partition table
  645.         cmp     word [ecx+0x40], 0xaa55
  646.         jnz     .mbr_failed
  647. ; 8. The MBR is treated differently from EBRs. For MBR we additionally need to
  648. ; execute step 9 and possibly step 10.
  649.         test    ebp, ebp
  650.         jnz     .mbr
  651. ; Partition table can be present or not present. In the first case, we just
  652. ; read the MBR. In the second case, we just read the bootsector for some
  653. ; filesystem.
  654. ; We use the following algorithm to distinguish between these cases.
  655. ; A. If at least one entry of the partition table is invalid, this is
  656. ;    a bootsector. See the description of 'is_partition_table_entry' for
  657. ;    definition of validity.
  658. ; B. If all entries are empty (filesystem type field is zero) and the first
  659. ;    byte is jmp opcode (0EBh or 0E9h), this is a bootsector which happens to
  660. ;    have zeros in the place of partition table.
  661. ; C. Otherwise, this is a MBR.
  662. ; 9. Test for MBR vs bootsector.
  663. ; 9a. Check entries. If any is invalid, go to 10 (rule A).
  664.         call    is_partition_table_entry
  665.         jc      .notmbr
  666.         add     ecx, 10h
  667.         call    is_partition_table_entry
  668.         jc      .notmbr
  669.         add     ecx, 10h
  670.         call    is_partition_table_entry
  671.         jc      .notmbr
  672.         add     ecx, 10h
  673.         call    is_partition_table_entry
  674.         jc      .notmbr
  675. ; 9b. Check types of the entries. If at least one is nonzero, go to 11 (rule C).
  676.         mov     al, [ecx-30h+PARTITION_TABLE_ENTRY.Type]
  677.         or      al, [ecx-20h+PARTITION_TABLE_ENTRY.Type]
  678.         or      al, [ecx-10h+PARTITION_TABLE_ENTRY.Type]
  679.         or      al, [ecx+PARTITION_TABLE_ENTRY.Type]
  680.         jnz     .mbr
  681. ; 9c. Empty partition table or bootsector with many zeroes? (rule B)
  682.         cmp     byte [ebx], 0EBh
  683.         jz      .notmbr
  684.         cmp     byte [ebx], 0E9h
  685.         jnz     .mbr
  686. .notmbr:
  687. ; 10. This is not MBR. The media is not partitioned. Create one partition
  688. ; which covers all the media and abort the loop.
  689.         stdcall disk_add_partition, 0, 0, \
  690.                 dword [esi+DISK.MediaInfo.Capacity], dword [esi+DISK.MediaInfo.Capacity+4]
  691.         jmp     .done
  692. .mbr:
  693. ; 11. Process all entries of the new MBR/EBR
  694.         lea     ecx, [ebx+0x1be]        ; ecx -> partition table
  695.         push    0       ; assume no extended partition
  696.         call    process_partition_table_entry
  697.         add     ecx, 10h
  698.         call    process_partition_table_entry
  699.         add     ecx, 10h
  700.         call    process_partition_table_entry
  701.         add     ecx, 10h
  702.         call    process_partition_table_entry
  703.         pop     ebp
  704. ; 12. Test whether we found a new EBR and should continue the loop.
  705. ; 12a. If there was no next EBR, return.
  706.         test    ebp, ebp
  707.         jz      .done
  708. ; Ok, we have EBR.
  709. ; 12b. EBRs addresses are relative to the start of extended partition.
  710. ; For simplicity, just abort if an 32-bit overflow occurs; large disks
  711. ; are most likely partitioned with GPT, not MBR scheme, since the precise
  712. ; calculation here would increase limit just twice at the price of big
  713. ; compatibility problems.
  714.         pop     eax     ; load extended partition
  715.         add     ebp, eax
  716. ; 12c. If extended partition has not yet started, start it.
  717.         test    eax, eax
  718.         jnz     @f
  719.         mov     eax, ebp
  720. @@:
  721. ; 12c. If the limit is not exceeded, continue the loop.
  722.         dec     dword [esp]
  723.         push    eax     ; store extended partition
  724.         jnz     .new_mbr
  725. .mbr_failed:
  726. .done:
  727. ; 13. Cleanup after the loop.
  728.         pop     eax     ; not important anymore
  729.         pop     eax     ; not important anymore
  730.         pop     ebp     ; restore ebp
  731. ; 14. Release the buffer.
  732. ; 14a. Test whether it is the global buffer or we have allocated it.
  733.         cmp     ebx, mbr_buffer
  734.         jz      .release_partition_buffer
  735. ; 14b. If we have allocated it, free it.
  736.         xchg    eax, ebx
  737.         call    free
  738.         jmp     .nothing
  739. ; 14c. Otherwise, release reference.
  740. .release_partition_buffer:
  741. lock    dec     [partition_buffer_users]
  742. .nothing:
  743. ; 15. Return.
  744.         ret
  745.  
  746. ; This is an internal function called from disk_scan_partitions. It checks
  747. ; whether the entry pointed to by ecx is a valid entry of partition table.
  748. ; The entry is valid if the first byte is 0 or 80h, the first sector plus the
  749. ; length is less than twice the size of media. Multiplication by two is
  750. ; required since the size mentioned in the partition table can be slightly
  751. ; greater than the real size.
  752. is_partition_table_entry:
  753. ; 1. Check .Bootable field.
  754.         mov     al, [ecx+PARTITION_TABLE_ENTRY.Bootable]
  755.         and     al, 7Fh
  756.         jnz     .invalid
  757. ; 3. Calculate first sector + length. Note that .FirstAbsSector is relative
  758. ; to the MBR/EBR, so the real sum is ebp + .FirstAbsSector + .Length.
  759.         mov     eax, ebp
  760.         xor     edx, edx
  761.         add     eax, [ecx+PARTITION_TABLE_ENTRY.FirstAbsSector]
  762.         adc     edx, 0
  763.         add     eax, [ecx+PARTITION_TABLE_ENTRY.Length]
  764.         adc     edx, 0
  765. ; 4. Divide by two.
  766.         shr     edx, 1
  767.         rcr     eax, 1
  768. ; 5. Compare with capacity. If the subtraction (edx:eax) - .Capacity does not
  769. ; overflow, this is bad.
  770.         sub     eax, dword [esi+DISK.MediaInfo.Capacity]
  771.         sbb     edx, dword [esi+DISK.MediaInfo.Capacity+4]
  772.         jnc     .invalid
  773. .valid:
  774. ; 5. Return success: CF is cleared.
  775.         clc
  776.         ret
  777. .invalid:
  778. ; 6. Return fail: CF is set.
  779.         stc
  780.         ret
  781.  
  782. ; This is an internal function called from disk_scan_partitions. It processes
  783. ; the entry pointed to by ecx.
  784. ; * If the entry is invalid, just ignore this entry.
  785. ; * If the type is zero, just ignore this entry.
  786. ; * If the type is one of types for extended partition, store the address
  787. ;   of this partition as the new MBR in [esp+4].
  788. ; * Otherwise, add the partition to the list of partitions for this disk.
  789. ;   We don't use the type from the entry to identify the file system;
  790. ;   fs-specific checks do this more reliably.
  791. process_partition_table_entry:
  792. ; 1. Check for valid entry. If invalid, return (go to 5).
  793.         call    is_partition_table_entry
  794.         jc      .nothing
  795. ; 2. Check for empty entry. If invalid, return (go to 5).
  796.         mov     al, [ecx+PARTITION_TABLE_ENTRY.Type]
  797.         test    al, al
  798.         jz      .nothing
  799. ; 3. Check for extended partition. If extended, go to 6.
  800. irp type,\
  801.     0x05,\                 ; DOS: extended partition
  802.     0x0f,\                 ; WIN95: extended partition, LBA-mapped
  803.     0xc5,\                 ; DRDOS/secured: extended partition
  804.     0xd5                   ; Old Multiuser DOS secured: extended partition
  805. {
  806.         cmp     al, type
  807.         jz      .extended
  808. }
  809. ; 4. If we are here, that is a normal partition. Add it to the list.
  810. ; Note that the first sector is relative to MBR/EBR.
  811.         mov     eax, ebp
  812.         xor     edx, edx
  813.         add     eax, [ecx+PARTITION_TABLE_ENTRY.FirstAbsSector]
  814.         adc     edx, 0
  815.         push    ecx
  816.         stdcall disk_add_partition, eax, edx, \
  817.                 [ecx+PARTITION_TABLE_ENTRY.Length], 0
  818.         pop     ecx
  819. .nothing:
  820. ; 5. Return.
  821.         ret
  822. .extended:
  823. ; 6. If we are here, that is an extended partition. Store the address.
  824.         mov     eax, [ecx+PARTITION_TABLE_ENTRY.FirstAbsSector]
  825.         mov     [esp+4], eax
  826.         ret
  827.  
  828. ; This is an internal function called from disk_scan_partitions and
  829. ; process_partition_table_entry. It adds one partition to the list of
  830. ; partitions for the media.
  831. proc disk_add_partition stdcall uses ebx edi, start:qword, length:qword
  832. ; 1. Check that this partition will not exceed the limit on total number.
  833.         cmp     [esi+DISK.NumPartitions], MAX_NUM_PARTITIONS
  834.         jae     .nothing
  835. ; 2. Check that this partition does not overlap with any already registered
  836. ; partition. Since any file system assumes that the disk data will not change
  837. ; outside of its control, such overlap could be destructive.
  838. ; Since the number of partitions is usually very small and is guaranteed not
  839. ; to be large, the simple linear search is sufficient.
  840. ; 2a. Prepare the loop: edi will point to the current item of .Partitions
  841. ; array, ecx will be the current item, ebx will hold number of items left.
  842.         mov     edi, [esi+DISK.Partitions]
  843.         mov     ebx, [esi+DISK.NumPartitions]
  844.         test    ebx, ebx
  845.         jz      .partitionok
  846. .scan_existing:
  847. ; 2b. Get the next partition.
  848.         mov     ecx, [edi]
  849.         add     edi, 4
  850. ; The range [.FirstSector, .FirstSector+.Length) must be either entirely to
  851. ; the left of [start, start+length) or entirely to the right.
  852. ; 2c. Subtract .FirstSector - start. The possible overflow distinguish between
  853. ; cases "to the left" (2?) and "to the right" (2d).
  854.         mov     eax, dword [ecx+PARTITION.FirstSector]
  855.         mov     edx, dword [ecx+PARTITION.FirstSector+4]
  856.         sub     eax, dword [start]
  857.         sbb     edx, dword [start+4]
  858.         jb      .less
  859. ; 2d. .FirstSector is greater than or equal to start. Check that .FirstSector
  860. ; is greater than or equal to start+length; the subtraction
  861. ; (.FirstSector-start) - length must not cause overflow. Go to 2g if life is
  862. ; good or to 2f in the other case.
  863.         sub     eax, dword [length]
  864.         sbb     edx, dword [length+4]
  865.         jb      .overlap
  866.         jmp     .next_existing
  867. .less:
  868. ; 2e. .FirstSector is less than start. Check that .FirstSector+.Length is less
  869. ; than or equal to start. If the addition (.FirstSector-start) + .Length does
  870. ; not cause overflow, then .FirstSector + .Length is strictly less than start;
  871. ; since the equality is also valid, use decrement preliminarily. Go to 2g or
  872. ; 2f depending on the overflow.
  873.         sub     eax, 1
  874.         sbb     edx, 0
  875.         add     eax, dword [ecx+PARTITION.Length]
  876.         adc     edx, dword [ecx+PARTITION.Length+4]
  877.         jnc     .next_existing
  878. .overlap:
  879. ; 2f. The partition overlaps with previously registered partition. Say warning
  880. ; and return with nothing done.
  881.         dbgstr 'two partitions overlap, ignoring the last one'
  882.         jmp     .nothing
  883. .next_existing:
  884. ; 2g. The partition does not overlap with the current partition. Continue the
  885. ; loop.
  886.         dec     ebx
  887.         jnz     .scan_existing
  888. .partitionok:
  889. ; 3. The partition has passed tests. Reallocate the partitions array for a new
  890. ; entry.
  891. ; 3a. Call the allocator.
  892.         mov     eax, [esi+DISK.NumPartitions]
  893.         inc     eax     ; one more entry
  894.         shl     eax, 2  ; each entry is dword
  895.         call    malloc
  896. ; 3b. Test the result. If failed, return with nothing done.
  897.         test    eax, eax
  898.         jz      .nothing
  899. ; 3c. Copy the old array to the new array.
  900.         mov     edi, eax
  901.         push    esi
  902.         mov     ecx, [esi+DISK.NumPartitions]
  903.         mov     esi, [esi+DISK.Partitions]
  904.         rep     movsd
  905.         pop     esi
  906. ; 3d. Set the field in the DISK structure to the new array.
  907.         xchg    [esi+DISK.Partitions], eax
  908. ; 3e. Free the old array.
  909.         call    free
  910. ; 4. Recognize the file system.
  911. ; 4a. Call the filesystem recognizer. It will allocate the PARTITION structure
  912. ; with possible filesystem-specific fields.
  913.         call    disk_detect_partition
  914. ; 4b. Check return value. If zero, return with list not changed; so far only
  915. ; the array was reallocated, this is ok for other code.
  916.         test    eax, eax
  917.         jz      .nothing
  918. ; 5. Insert the new partition to the list.
  919.         stosd
  920.         inc     [esi+DISK.NumPartitions]
  921. ; 6. Return.
  922. .nothing:
  923.         ret
  924. endp
  925.  
  926. ; This is an internal function called from disk_add_partition.
  927. ; It tries to recognize the file system on the partition and allocates the
  928. ; corresponding PARTITION structure with filesystem-specific fields.
  929. disk_detect_partition:
  930. ; This function inherits the stack frame from disk_add_partition. In stdcall
  931. ; with ebp-based frame arguments start from ebp+8, since [ebp]=saved ebp
  932. ; and [ebp+4]=return address.
  933. virtual at ebp+8
  934. .start  dq      ?
  935. .length dq      ?
  936. end virtual
  937. ; Currently no file systems are supported, so just allocate the PARTITION
  938. ; structure without extra fields.
  939. ; 1. Allocate and check result.
  940.         push    sizeof.PARTITION
  941.         pop     eax
  942.         call    malloc
  943.         test    eax, eax
  944.         jz      .nothing
  945. ; 2. Fill the common fields: copy .start and .length.
  946.         mov     edx, dword [.start]
  947.         mov     dword [eax+PARTITION.FirstSector], edx
  948.         mov     edx, dword [.start+4]
  949.         mov     dword [eax+PARTITION.FirstSector+4], edx
  950.         mov     edx, dword [.length]
  951.         mov     dword [eax+PARTITION.Length], edx
  952.         mov     edx, dword [.length+4]
  953.         mov     dword [eax+PARTITION.Length+4], edx
  954. .nothing:
  955. ; 3. Return with eax = pointer to PARTITION or NULL.
  956.         ret
  957.  
  958. ; This function is called from file_system_lfn.
  959. ; This handler gets the control each time when fn 70 is called
  960. ; with unknown item of root subdirectory.
  961. ; in: esi -> name
  962. ;     ebp = 0 or rest of name relative to esi
  963. ; out: if the handler processes path, it must not return in file_system_lfn,
  964. ;      but instead pop return address and return directly to the caller
  965. ;      otherwise simply return
  966. dyndisk_handler:
  967.         push    ebx edi         ; save registers used in file_system_lfn
  968. ; 1. Acquire the mutex.
  969.         mov     ebx, disk_list_mutex
  970.         call    wait_mutex
  971. ; 2. Loop over the list of DISK structures.
  972. ; 2a. Initialize.
  973.         mov     ecx, disk_list
  974. .scan:
  975. ; 2b. Get the next item.
  976.         mov     ecx, [ecx+DISK.Next]
  977. ; 2c. Check whether the list is done. If so, go to 3.
  978.         cmp     ecx, disk_list
  979.         jz      .notfound
  980. ; 2d. Compare names. If names match, go to 5.
  981.         mov     edi, [ecx+DISK.Name]
  982.         push    esi
  983. @@:
  984. ; esi points to the name from fs operation; it is terminated by zero or slash.
  985.         lodsb
  986.         test    al, al
  987.         jz      .eoin_dec
  988.         cmp     al, '/'
  989.         jz      .eoin
  990. ; edi points to the disk name.
  991.         inc     edi
  992. ; edi points to lowercase name, this is a requirement for the driver.
  993. ; Characters at esi can have any register. Lowercase the current character.
  994. ; This lowercasing works for latin letters and digits; since the disk name
  995. ; should not contain other symbols, this is ok.
  996.         or      al, 20h
  997.         cmp     al, [edi-1]
  998.         jz      @b
  999. .wrongname:
  1000. ; 2f. Names don't match. Continue the loop.
  1001.         pop     esi
  1002.         jmp     .scan
  1003. .notfound:
  1004. ; The loop is done and no name matches.
  1005. ; 3. Release the mutex.
  1006.         mov     dword [ebx], 0
  1007. ; 4. Return normally.
  1008.         pop     edi ebx         ; restore registers used in file_system_lfn
  1009.         ret
  1010. ; part of 2d: the name matches partially, but we must check that this is full
  1011. ; equality.
  1012. .eoin_dec:
  1013.         dec     esi
  1014. .eoin:
  1015.         cmp     byte [edi], 0
  1016.         jnz     .wrongname
  1017. ; We found the addressed DISK structure.
  1018. ; 5. Reference the disk.
  1019. lock    inc     [ecx+DISK.RefCount]
  1020. ; 6. Now we are sure that the DISK structure is not going to die at least
  1021. ; while we are working with it, so release the global mutex.
  1022.         mov     dword [ebx], 0
  1023. ; 7. Acquire the mutex for media object.
  1024.         pop     edi             ; restore edi
  1025.         lea     ebx, [ecx+DISK.MediaLock]
  1026.         call    wait_mutex
  1027. ; 8. Get the media object. If it is not NULL, reference it.
  1028.         xor     edx, edx
  1029.         cmp     [ecx+DISK.MediaInserted], dl
  1030.         jz      @f
  1031.         mov     edx, ecx
  1032.         inc     [ecx+DISK.MediaRefCount]
  1033. @@:
  1034. ; 9. Now we are sure that the media object, if it exists, is not going to die
  1035. ; at least while we are working with it, so release the mutex for media object.
  1036.         mov     dword [ebx], 0
  1037.         pop     ebx eax         ; restore ebx, pop return address
  1038. ; 10. Check whether the fs operation wants to enumerate partitions (go to 11)
  1039. ; or work with some concrete partition (go to 12).
  1040.         cmp     byte [esi], 0
  1041.         jnz     .haspartition
  1042. ; 11. The fs operation wants to enumerate partitions.
  1043. ; 11a. Only "list directory" operation is applicable to /<diskname> path. Check
  1044. ; the operation code. If wrong, go to 13.
  1045.         cmp     dword [ebx], 1
  1046.         jnz     .access_denied
  1047. ; 11b. If the media is inserted, use 'fs_dyndisk_next' as an enumeration
  1048. ; procedure. Otherwise, use 'fs_dyndisk_next_nomedia'.
  1049.         mov     esi, fs_dyndisk_next_nomedia
  1050.         test    edx, edx
  1051.         jz      @f
  1052.         mov     esi, fs_dyndisk_next
  1053. @@:
  1054. ; 11c. Let the procedure from fs_lfn.inc do the job.
  1055.         jmp     file_system_lfn.maindir_noesi
  1056. .haspartition:
  1057. ; 12. The fs operation has specified some partition.
  1058. ; 12a. Store parameters for callback functions.
  1059.         push    edx
  1060.         push    ecx
  1061. ; 12b. Store callback functions.
  1062.         push    dyndisk_cleanup
  1063.         push    fs_dyndisk
  1064.         mov     edi, esp
  1065. ; 12c. Let the procedure from fs_lfn.inc do the job.
  1066.         jmp     file_system_lfn.found2
  1067. .access_denied:
  1068. ; 13. Fail the operation with the appropriate code.
  1069.         mov     dword [esp+32], ERROR_ACCESS_DENIED
  1070. .cleanup:
  1071. ; 14. Cleanup.
  1072.         mov     esi, ecx        ; disk*dereference assume that esi points to DISK
  1073. .cleanup_esi:
  1074.         test    edx, edx        ; if there are no media, we didn't reference it
  1075.         jz      @f
  1076.         call    disk_media_dereference
  1077. @@:
  1078.         call    disk_dereference
  1079. ; 15. Return.
  1080.         ret
  1081.  
  1082. ; This is a callback for cleaning up things called from file_system_lfn.found2.
  1083. dyndisk_cleanup:
  1084.         mov     esi, [edi+8]
  1085.         mov     edx, [edi+12]
  1086.         jmp     dyndisk_handler.cleanup_esi
  1087.  
  1088. ; This is a callback for enumerating partitions called from
  1089. ; file_system_lfn.maindir in the case of inserted media.
  1090. ; It just increments eax until DISK.NumPartitions reached and then
  1091. ; cleans up.
  1092. fs_dyndisk_next:
  1093.         cmp     eax, [ecx+DISK.NumPartitions]
  1094.         jae     .nomore
  1095.         inc     eax
  1096.         clc
  1097.         ret
  1098. .nomore:
  1099.         pusha
  1100.         mov     esi, ecx
  1101.         call    disk_media_dereference
  1102.         call    disk_dereference
  1103.         popa
  1104.         stc
  1105.         ret
  1106.  
  1107. ; This is a callback for enumerating partitions called from
  1108. ; file_system_lfn.maindir in the case of missing media.
  1109. ; In this case we create one pseudo-partition.
  1110. fs_dyndisk_next_nomedia:
  1111.         cmp     eax, 1
  1112.         jae     .nomore
  1113.         inc     eax
  1114.         clc
  1115.         ret
  1116. .nomore:
  1117.         pusha
  1118.         mov     esi, ecx
  1119.         call    disk_dereference
  1120.         popa
  1121.         stc
  1122.         ret
  1123.  
  1124. ; This is a callback for doing real work with selected partition.
  1125. ; Currently this is just placeholder, since no file systems are supported.
  1126. ; edi = esp -> {dd fs_dyndisk, dd dyndisk_cleanup, dd pointer to DISK, dd media object}
  1127. ; ecx = partition number, esi+ebp = ASCIIZ name
  1128. fs_dyndisk:
  1129.         dec     ecx     ; convert to zero-based partition index
  1130.         pop     edx edx edx eax ; edx = pointer to DISK, eax = NULL or edx
  1131.         test    eax, eax
  1132.         jz      .nomedia
  1133. .main:
  1134.         cmp     ecx, [edx+DISK.NumPartitions]
  1135.         jae     .notfound
  1136.         mov     dword [esp+32], ERROR_UNKNOWN_FS
  1137. .cleanup:
  1138.         mov     esi, edx
  1139.         call    disk_media_dereference
  1140.         call    disk_dereference
  1141.         ret
  1142. .notfound:
  1143.         mov     dword [esp+32], ERROR_FILE_NOT_FOUND
  1144.         jmp     .cleanup
  1145. .nomedia:
  1146.         test    ecx, ecx
  1147.         jnz     .notfound
  1148.         test    byte [edx+DISK.DriverFlags], DISK_NO_INSERT_NOTIFICATION
  1149.         jz      .deverror
  1150. ; if the driver does not support insert notifications and we are the only fs
  1151. ; operation with this disk, issue the fake insert notification; if media is
  1152. ; still not inserted, 'disk_media_changed' will detect this and do nothing
  1153.         push    ebx
  1154.         lea     ebx, [edx+DISK.MediaLock]
  1155.         call    wait_mutex
  1156.         cmp     [edx+DISK.MediaRefCount], 1
  1157.         jnz     .noluck
  1158.         mov     dword [ebx], 0
  1159.         push    edx
  1160.         stdcall disk_media_changed, edx, 1
  1161.         pop     edx
  1162.         call    wait_mutex
  1163.         cmp     [edx+DISK.MediaInserted], 0
  1164.         jz      .noluck
  1165. lock    inc     [edx+DISK.MediaRefCount]
  1166.         mov     dword [ebx], 0
  1167.         xor     ecx, ecx
  1168.         jmp     .main
  1169. .noluck:
  1170.         mov     dword [ebx], 0
  1171. .deverror:
  1172.         mov     dword [esp+32], ERROR_DEVICE
  1173.         mov     esi, edx
  1174.         call    disk_dereference
  1175.         ret
  1176.  
  1177. ; This function is called from file_system_lfn.
  1178. ; This handler is called when virtual root is enumerated
  1179. ; and must return all items which can be handled by this.
  1180. ; It is called several times, first time with eax=0
  1181. ; in: eax = 0 for first call, previously returned value for subsequent calls
  1182. ; out: eax = 0 => no more items
  1183. ;      eax != 0 => buffer pointed to by edi contains name of item
  1184. dyndisk_enum_root:
  1185.         push    ebx             ; save register used in file_system_lfn
  1186.         mov     ebx, disk_list_mutex    ; it will be useful
  1187. ; 1. If this is the first call, acquire the mutex and initialize.
  1188.         test    eax, eax
  1189.         jnz     .notfirst
  1190.         call    wait_mutex
  1191.         mov     eax, disk_list
  1192. .notfirst:
  1193. ; 2. Get next item.
  1194.         mov     eax, [eax+DISK.Next]
  1195. ; 3. If there are no more items, go to 6.
  1196.         cmp     eax, disk_list
  1197.         jz      .last
  1198. ; 4. Copy name from the DISK structure to edi.
  1199.         push    eax esi
  1200.         mov     esi, [eax+DISK.Name]
  1201. @@:
  1202.         lodsb
  1203.         stosb
  1204.         test    al, al
  1205.         jnz     @b
  1206.         pop     esi eax
  1207. ; 5. Return with eax = item.
  1208.         pop     ebx             ; restore register used in file_system_lfn
  1209.         ret
  1210. .last:
  1211. ; 6. Release the mutex and return with eax = 0.
  1212.         xor     eax, eax
  1213.         mov     dword [ebx], eax
  1214.         pop     ebx             ; restore register used in file_system_lfn
  1215.         ret
  1216.