Subversion Repositories Kolibri OS

Rev

Rev 7756 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ; Search Additional Partition for KolibriOS applications
  2. ;
  3. ; Copyright (c) 2013-2014, Marat Zakiyanov aka Mario79, aka Mario
  4. ; All rights reserved.
  5. ;
  6. ; Redistribution and use in source and binary forms, with or without
  7. ; modification, are permitted provided that the following conditions are met:
  8. ;        * Redistributions of source code must retain the above copyright
  9. ;          notice, this list of conditions and the following disclaimer.
  10. ;        * Redistributions in binary form must reproduce the above copyright
  11. ;          notice, this list of conditions and the following disclaimer in the
  12. ;          documentation and/or other materials provided with the distribution.
  13. ;        * Neither the name of the <organization> nor the
  14. ;          names of its contributors may be used to endorse or promote products
  15. ;          derived from this software without specific prior written permission.
  16. ;
  17. ; THIS SOFTWARE IS PROVIDED BY Marat Zakiyanov ''AS IS'' AND ANY
  18. ; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. ; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. ; DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  21. ; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. ; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. ; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. ;*****************************************************************************
  28.         use32
  29.         org     0x0
  30.  
  31.         db 'MENUET01'
  32.         dd 0x01
  33.         dd START
  34.         dd IM_END
  35.         dd I_END
  36.         dd stacktop
  37. params dd PARAMS
  38.         dd 0x0
  39. ;---------------------------------------------------------------------
  40. delay dd 500
  41. mount_attempt dd 0
  42. ;---------------------------------------------------------------------
  43. fileinfo:
  44. .subfunction    dd 5
  45. .Offset         dd 0
  46. .Offset_1       dd 0
  47. .size           dd 0
  48. .return         dd folder_data
  49.                 db 0
  50. .name:          dd basic_file_path
  51. ;---------------------------------------------------------------------
  52. read_folder:
  53. .subfunction    dd 1
  54. .start          dd 0
  55. .flags          dd 0
  56. .size           dd 32
  57. .return         dd folder_data
  58.                 db 0
  59. .name:          dd read_folder_name
  60. ;---------------------------------------------------------------------
  61. read_folder_1:
  62. .subfunction    dd 1
  63. .start          dd 0
  64. .flags          dd 0
  65. .size           dd 32
  66. .return         dd folder_data_1
  67.                 db 0
  68. .name:          dd read_folder_1_name
  69. ;---------------------------------------------------------------------
  70. start_dir:
  71.         db '/',0
  72. ;-------------------------------------------------------------------------------
  73. basic_file_path:
  74.         db '/sys/settings/'
  75. basic_file_name:
  76.         db 'kolibrios/res/system/kolibri.lbl',0
  77. additional_dir_name:
  78.     db 'kolibrios',0
  79. real_additional_dir:
  80.         db '/kolibrios',0
  81. ;-------------------------------------------------------------------------------
  82. include '../../macros.inc'
  83.  
  84. define __DEBUG__ 1
  85. define __DEBUG_LEVEL__ 2        ; 1 = verbose, 2 = main only
  86. include "../../debug-fdo.inc"
  87. ;-------------------------------------------------------------------------------
  88. START:
  89. ; process cmdline params
  90.         mov     esi, [params]
  91.         test    [esi], byte 0xFF
  92.         jz      .params_done
  93.         cmp     word[esi], '-d' ; delay
  94.         jne     .params_done
  95.         add     esi, 2
  96. ; str2uint(delay)
  97.         xor     eax, eax
  98.         xor     ecx, ecx
  99. .convert:
  100.         lodsb
  101.         test    al, al
  102.         jz      .converted
  103.         lea     ecx, [ecx + ecx * 4]
  104.         lea     ecx, [eax + ecx * 2 - '0']
  105.         jmp     .convert
  106. .converted:
  107.         mov     [delay], ecx
  108. ;--------------------------------------
  109.         DEBUGF  1, "Searchap: get basic file\n"
  110.         call    load_file       ; download the master file
  111.         xor     eax,eax
  112.         cmp     [fs_error],eax
  113.         jne     exit
  114.         mov     eax,[fileinfo.size]
  115.         mov     [basic_file_size],eax
  116. .params_done:
  117. ;--------------------------------------
  118.         mov     ebx,start_dir
  119.         mov     ax,[ebx]
  120.         mov     ebx,read_folder_name
  121.         mov     [ebx],ax
  122.         mov     ebx,read_folder_1_name
  123.         mov     [ebx],ax
  124.         call    device_detect_f70
  125. ;--------------------------------------
  126.         call    print_retrieved_devices_table
  127.         call    search_and_load_pointer_file_label
  128. ;---------------------------------------------------------------------
  129. exit:
  130. ;--------------------------------------
  131.         DEBUGF  1, "Searchap: just exit\n"
  132. ;--------------------------------------
  133.         cmp [mount_dir],1
  134.         je @f
  135.         cmp [mount_attempt], 1
  136.         je @f
  137.         mov [mount_attempt], 1 ;second mount attempt with delay
  138.         DEBUGF  2, "Searchap: second attempt after 5 seconds!\n"
  139.         mcall   5,[delay]
  140.         jmp START.params_done
  141. @@:
  142.         mcall   -1
  143. ;---------------------------------------------------------------------
  144. device_detect_f70:
  145. ;--------------------------------------
  146.         DEBUGF  1, "Searchap: read_folder_name: %s\n",read_folder_name
  147. ;--------------------------------------
  148.         mcall   70,read_folder
  149.         test    eax,eax
  150.         jz      @f
  151.         cmp     eax,6
  152.         je      @f
  153. ;--------------------------------------
  154.         DEBUGF  1, "Searchap: read_folder_error\n"
  155. ;--------------------------------------
  156.         jmp     exit
  157. @@:
  158. ;--------------------------------------
  159.         call    print_root_dir
  160. ;--------------------------------------
  161.         mov     [left_folder_block],ebx
  162.         xor     eax,eax
  163.         mov     [temp_counter_1],eax
  164.         mov     [retrieved_devices_table_counter],eax
  165. .start_temp_counter_1:
  166.         imul    esi,[temp_counter_1],304
  167.         add     esi,[read_folder.return]
  168.         add     esi,32+40
  169.         call    copy_folder_name_1
  170. ;--------------------------------------
  171.         DEBUGF  1, "Searchap: read_folder_1_name: %s\n",read_folder_1_name
  172. ;--------------------------------------
  173.         mcall   70,read_folder_1
  174.         test    eax,eax
  175.         jz      @f
  176.         cmp     eax,6
  177.         je      @f
  178. ;--------------------------------------
  179.         DEBUGF  1, "Searchap: read_folder_error_1\n"
  180. ;--------------------------------------
  181.         jmp     exit
  182. @@:
  183.         mov     eax,[read_folder_1.return]
  184.         cmp     [eax+4],dword 0
  185.         je      .continue
  186.         mov     [right_folder_block],ebx
  187.         xor     ebp,ebp
  188. .start_copy_device_patch:
  189.         imul    edi,[retrieved_devices_table_counter],10
  190.         add     edi,retrieved_devices_table
  191.         mov     [edi],byte '/'
  192.         inc     edi
  193.         imul    esi,[temp_counter_1],304
  194.         add     esi,[read_folder.return]
  195.         add     esi,32+40
  196.         call    proc_copy_patch
  197.         imul    esi,ebp,304
  198.         add     esi,[read_folder_1.return]
  199.         add     esi,32+40
  200.         mov     [edi-1],byte '/'
  201.         call    proc_copy_patch
  202.         inc     [retrieved_devices_table_counter]
  203.         inc     ebp
  204.         cmp     ebp,[right_folder_block]
  205.         jb      .start_copy_device_patch
  206. .continue:
  207.         inc     [temp_counter_1]
  208.         mov     eax,[temp_counter_1]
  209.         cmp     eax,[left_folder_block]
  210.         jb      .start_temp_counter_1
  211.         mov     esi,retrieved_devices_table+1
  212.         call    copy_folder_name
  213.         mov     esi,retrieved_devices_table+3
  214.         xor     ecx,ecx
  215. @@:
  216.         add     esi,8
  217.         cld
  218.         lodsw
  219.         inc     ecx
  220.         cmp     ecx,[retrieved_devices_table_counter]
  221.         ja      @f
  222.         cmp     ax,'hd'
  223.         jne     @r
  224.         sub     esi,2
  225.         call    copy_folder_name_1
  226.         ret
  227. @@:
  228.         mov     esi,retrieved_devices_table+1
  229.         call    copy_folder_name_1
  230.         ret
  231. ;---------------------------------------------------------------------
  232. load_file:
  233.         mov     [fileinfo.subfunction],dword 5
  234.         xor     eax,eax
  235.         mov     [fileinfo.size],eax
  236.         mov     [fs_error],eax
  237. ;--------------------------------------
  238.         DEBUGF  1, "Searchap: get file info\n"
  239. ;--------------------------------------
  240.         mcall   68,1
  241.         mcall   70,fileinfo
  242.         mov     [fs_error],eax
  243.         test    eax,eax
  244.         jnz     .file_error
  245. ;--------------------------------------
  246.         DEBUGF  1, "Searchap: file info ok\n"
  247. ;--------------------------------------
  248.         xor     eax,eax
  249.         mov     [fileinfo.subfunction],eax      ;dword 0
  250.         mov     eax,[fileinfo.return]
  251.         mov     ecx,[eax+32]
  252. ;--------------------------------------
  253.         DEBUGF  1, "Searchap: real file size: %d\n",ecx
  254. ;--------------------------------------
  255.         test    ecx,ecx
  256.         jz      .file_error
  257.         mov     eax,304*32+32 ; 9 Kb
  258.         cmp     ecx,eax
  259.         jbe     @f
  260.         mov     ecx,eax
  261. ;-----------------------------------
  262. @@:    
  263.         mov     [fileinfo.size],ecx
  264. ;--------------------------------------
  265.         DEBUGF  1, "Searchap: get file\n"
  266. ;--------------------------------------
  267.         mcall   68,1
  268.         mcall   70,fileinfo
  269.         mov     [fs_error],eax
  270.         test    eax,eax
  271.         jz      @f
  272. ;       cmp     eax,6
  273. ;       jne     .file_error
  274. ;       xor     eax,eax
  275. ;       mov     [fs_error],eax
  276. ;       jmp     @f
  277. ;-----------------------------------
  278. .file_error:
  279. ;--------------------------------------
  280.         DEBUGF  1, "Searchap: read file - error!\n"
  281. ;--------------------------------------
  282.         ret
  283. ;-----------------------------------   
  284. @@:
  285. ;--------------------------------------
  286.         DEBUGF  1, "Searchap: read file corrected size: %d\n",[fileinfo.size]
  287. ;--------------------------------------
  288.         ret
  289. ;---------------------------------------------------------------------
  290. search_and_load_pointer_file_label:
  291.         mov     [fileinfo.return],dword folder_data_1
  292.         mov     ecx,[retrieved_devices_table_counter]
  293.         dec     ecx     ; /rd/1/ no need to check
  294.         mov     [fileinfo.name],dword read_folder_name
  295.         mov     esi,retrieved_devices_table
  296. ;       sub     esi,10  ; deleted because /rd/1/ no need to check
  297. .next_entry:
  298. ;--------------------------------------
  299.         DEBUGF  1, "\nSearchap: copy next entry\n"
  300. ;--------------------------------------
  301.         add     esi,10
  302.         push    esi
  303.         add     esi,1
  304.         call    copy_folder_name
  305.         mov     esi,basic_file_name-1
  306.         dec     edi
  307.         call    copy_folder_name.1
  308.         pop     esi
  309. ;--------------------------------------
  310.         DEBUGF  1, "Searchap: %s\n",dword[fileinfo.name]
  311. ;--------------------------------------
  312. ;       mcall   5,10
  313.         push    ecx
  314.         call    load_file
  315.         pop     ecx
  316.  
  317.         xor     eax,eax
  318.         cmp     [fs_error],eax
  319.         jne     @f
  320.         mov     eax,[fileinfo.size]
  321.         cmp     eax,[basic_file_size]
  322.         jae     .sucess
  323. @@:
  324.         dec     ecx
  325.         jnz     .next_entry
  326. ;--------------------------------------
  327.         DEBUGF  2, "Searchap: additional partition is not found!\n"
  328. ;--------------------------------------
  329.         ret
  330. .sucess:
  331.         ;call   compare_files_and_mount
  332.         call    compare_files_and_mount.mount_now ;no need to compare files content
  333.         cmp     [compare_flag],byte 0
  334.         jne     @b
  335.         cmp     [mount_dir],1
  336.         je      @f
  337.         DEBUGF  2, "Searchap: sorry, but the additional partition is not found!\n"
  338. @@:
  339.         ret
  340. ;---------------------------------------------------------------------
  341. compare_files_and_mount:
  342.         push    ecx esi
  343.         mov     ecx,[basic_file_size]
  344.         mov     esi,folder_data
  345.         mov     edi,folder_data_1
  346. .next_char:
  347.         cld
  348.         lodsb
  349.         mov     ah,[edi]
  350.         inc     edi
  351.         cmp     al,ah
  352.         jne     .not_match
  353.         dec     ecx
  354.         jnz     .next_char
  355.         pop     esi ecx
  356. .mount_now:
  357.         mov     [compare_flag],byte 0
  358. ;--------------------------------------
  359.         DEBUGF  2, "Searchap: compare files - success!\n"
  360.         DEBUGF  2, "Searchap: mount directory: %s\n",esi
  361.         mov     [mount_dir],1
  362. ;--------------------------------------
  363. ; prepare real directory path for mounting
  364.         inc     esi
  365.         mov     edi,f30_3_work_area+64
  366.         call    proc_copy_patch
  367.         dec     edi
  368.         mov     esi,real_additional_dir
  369.         call    proc_copy_patch
  370. ; prepare fake directory name
  371.         mov     esi,additional_dir_name
  372.         mov     edi,f30_3_work_area
  373.         call    proc_copy_patch
  374. ; here is call kernel function to mount the found partition
  375. ; as "/kolibrios" directory to root directory "/"
  376.         mcall   30,3,f30_3_work_area
  377.         ret
  378. ;--------------------------------------
  379. .not_match:
  380.         mov     [compare_flag],byte 1
  381.         pop     esi ecx
  382. ;--------------------------------------
  383.         DEBUGF  1, "Searchap: compared files doesn't match!\n"
  384. ;--------------------------------------
  385.         ret
  386. ;---------------------------------------------------------------------
  387. copy_folder_name:
  388.         mov     edi,read_folder_name+1
  389. .1:
  390. proc_copy_patch:
  391.         cld
  392. @@:
  393.         lodsb
  394.         stosb
  395.         test    al,al
  396.         jnz     @r
  397.         ret
  398. ;---------------------------------------------------------------------
  399. copy_folder_name_1:
  400.         mov     edi,read_folder_1_name+1
  401.         jmp     proc_copy_patch
  402. ;---------------------------------------------------------------------
  403. print_retrieved_devices_table:
  404.         mov     ecx,[retrieved_devices_table_counter]
  405.         mov     edx,retrieved_devices_table
  406.         DEBUGF  1, "Searchap: retrieved_devices_table:\n"
  407.         DEBUGF  1, "Searchap: ----------\n"
  408. @@:
  409.         DEBUGF  1, "Searchap: %s\n",edx
  410.         add     edx,10
  411.         dec     ecx
  412.         jnz     @b
  413.         DEBUGF  1, "\nSearchap: ----------\n"
  414.         ret
  415. ;---------------------------------------------------------------------
  416. print_root_dir:
  417.         DEBUGF  1, "Searchap: ----------\n"
  418.         DEBUGF  1, "Searchap: root dir:\n"
  419.         DEBUGF  1, "Searchap: ----------\n"
  420.         pusha
  421.         mov     ecx,ebx
  422.         mov     edx,folder_data+32+40
  423. @@:
  424.         DEBUGF  1, "Searchap: %s\n",edx
  425.         add     edx,304
  426.         dec     ecx
  427.         jnz     @b
  428.         popa
  429.         DEBUGF  1, "\nSearchap: ----------\n"
  430.         ret
  431. ;-------------------------------------------------------------------------------
  432. include_debug_strings
  433. ;-------------------------------------------------------------------------------
  434. IM_END:
  435. ;-------------------------------------------------------------------------------
  436. align 4
  437. PARAMS: rb 256
  438. align 4
  439. left_folder_block       rd 1
  440. right_folder_block      rd 1
  441. temp_counter_1          rd 1
  442. retrieved_devices_table_counter rd 1
  443. basic_file_size         rd 1
  444. fs_error                rd 1
  445. compare_flag            rb 1
  446. mount_dir               rb 1
  447. ;-------------------------------------------------------------------------------
  448. align 4
  449. f30_3_work_area:
  450.         rb 128
  451. ;-------------------------------------------------------------------------------
  452. align 4
  453. retrieved_devices_table:
  454.         rb 10*100
  455. ;-------------------------------------------------------------------------------
  456. align 4
  457. read_folder_name:
  458.         rb 256
  459. ;-------------------------------------------------------------------------------
  460. align 4
  461. read_folder_1_name:
  462.         rb 256
  463. ;-------------------------------------------------------------------------------
  464. align 4
  465. folder_data:
  466.         rb 304*32+32 ; 9 Kb
  467. ;-------------------------------------------------------------------------------
  468. align 4
  469. folder_data_1:
  470.         rb 304*32+32 ; 9 Kb
  471. ;-------------------------------------------------------------------------------
  472. align 4
  473.         rb 512
  474. stacktop:
  475. ;-------------------------------------------------------------------------------
  476. I_END:
  477. ;-------------------------------------------------------------------------------