Trustzone安全內核SierraTEE將根文件系統從虛擬盤移至sd卡方案

s3c6410 支持Nand Flash本地啓動Linux,包括內核,根文件系統,bootloader均寫入在Nand Flash.這樣可以獨立運行.

  很多情況下,Nand Flash的某種原因無法寫入內核和Rootfs.而使用tftp下載內核,用NFS啓動根文件系統比較慢.s3c6410 支持 SD卡啓動,經過改造u-boot可以從SD卡引導內核,這樣而Linux 又可以從SD卡的裝載ext3根文件系統.這樣可以製作一個完整的SD卡啓動卡.


  這樣bootloader(u-boot)可以寫入SD卡,引導扇區.
 
    bootloader的第一階段把自己裝入在內存高端地址看後,可以用兩種方法從SD卡裝入內核到內存.
  一種是用fatload命令從fat分區裝入內核文件到內存。
      fatload mmc 1 address filename
    (上述命令表示從第一個mmc設備裝入文件filename到內存的address當中),這個需要完整的實現fat的驅動,因此很多u-boot沒有帶.
    另外一種常見的做法是使用u-boot的movi 命令.
    用movi 命令直接讀寫SD卡sector.但是這樣有可以與文件系統有衝突,因此要小心分區.
    後面就是用這個方法來實現啓動的.可以把movi讀入命令寫入bootcmd 實現自動啓動

  3.內核裝入後,可以支持用ext3的格式作根文件系統,這樣可以把SD卡一個分區格式化成ext3即
    可以啓動 ,這樣要修改bootargs 

     這個可以參考<<ARM-Linux使用SD卡根文件系統>>
 

2.Linux SD分區操作.

   支持這種需要2G以下,非SDHC卡,假設是一個2G SD卡,分300M給FAT,剩下全部給 EXT3格式,作根文件系統。
   bootloader寫在SD卡的引導區,不佔用分區空間
   
   Linux 對SD卡的的操作,如果第一個插入的SD卡是 /dev/sda,第二個SD卡是/dev/sdb ...依次類推.
   如果第一個SD卡的第一個分區是 /dev/sda1 ,第二個分區 /dev/sda2 .如果上一個卡(sda)未安全撥下,而第二次插入也會變成sdb. 
   所以插入卡時要小心檢測SD卡對應的設備結點.


   2.1 Linux 分區工具 fdisk.

     fdisk 與dos下的工具同名,在操作SD分區,最好把分區文件目錄umount掉,否則會修改分區失敗,也注意要把SD卡的寫保護鎖打開. 命令格式    
      fdisk <設備結點> 
     這是指設備結點/dev/sda之類,不要對/dev/sda1 之類操作.

  1. fdisk /dev/sda
  2. The number of cylinders for this disk is set to 35560.
  3. There is nothing wrong with that, but this is larger than 1024,
  4. and could in certain setups cause problems with:
  5. 1) software that runs at boot time (e.g., old versions of LILO)
  6. 2) booting and partitioning software from other OSs
  7. (e.g., DOS FDISK, OS/2 FDISK)
  8. Command (m for help): m   #<------------顯示fdisk菜單
  9. Command action
  10. a toggle a bootable flag
  11. b edit bsd disklabel
  12. c toggle the dos compatibility flag
  13. d delete a partition
  14. l list known partition types
  15. m print this menu
  16. n add a new partition
  17. o create a new empty DOS partition table
  18. p print the partition table
  19. q quit without saving changes
  20. s create a new empty Sun disklabel
  21. t change a partition's system id
  22. u change display/entry units
  23. v verify the partition table
  24. w write table to disk and exit
  25. x extra functionality (experts only)
  26. Command (m for help): p #<--------------------打印分區表
  27. Disk /dev/sda: 2002 MB, 2002780160 bytes
  28. 11 heads, 10 sectors/track, 35560 cylinders
  29. Units = cylinders of 110 * 512 = 56320 bytes
  30. Device Boot Start End Blocks Id System
  31. /dev/sda1 2 35561 1955775+ b W95 FAT32  #原有的FAT32分區
接下操作,是刪除原有分區,並且新建兩個分區,一個用FAT,一個是EXT32根文件系統

  1. Command (m for help): d #<-------------刪除一個分區
  2. Selected partition 1
  3. Command (m for help): n #<-------------新建一個分區
  4. Command action
  5. e extended
  6. p primary partition (1-4)
  7. p #<--------------選擇創建 primary 首要分區
  8. Partition number (1-4): 1 #<---------第一個首要分區
  9. First cylinder (1-35560, default 1): #<---------起始位置,直接回車用缺省值
  10. Using default value 1
  11. Last cylinder or +size or +sizeM or +sizeK (1-35560, default 35560): 300M
  12. #<---- 第一個分區使用 300M空間
  13. Command (m for help): p
  14. Disk /dev/sda: 2002 MB, 2002780160 bytes
  15. 11 heads, 10 sectors/track, 35560 cylinders
  16. Units = cylinders of 110 * 512 = 56320 bytes
  17. Device Boot Start End Blocks Id System
  18. /dev/sda1 1 300 16495 83 Linux
  19. Command (m for help): n #<----建第二個分區
  20. Command action
  21. e extended
  22. p primary partition (1-4)
  23. p #<--------------選擇創建 primary 首要分區
  24. Partition number (1-4): 2 #<---------第二個首要分區
  25. First cylinder (301-35560, default 301):
  26. Using default value 301
  27. Last cylinder or +size or +sizeM or +sizeK (301-35560, default 35560):
  28. Using default value 35560 #直接回車表示缺省值,即把剩下所有空間都歸入這個分區
  29. Command (m for help): p
  30. Disk /dev/sda: 2002 MB, 2002780160 bytes
  31. 11 heads, 10 sectors/track, 35560 cylinders
  32. Units = cylinders of 110 * 512 = 56320 bytes
  33. Device Boot Start End Blocks Id System
  34. /dev/sda1 1 300 16495 83 Linux
  35. /dev/sda2 301 35560 1939300 83 Linux
  36. Command (m for help): w #<-------將分區表寫入SD卡,至此才真正生效
  37. The partition table has been altered!
  38. Calling ioctl() to re-read partition table.
  39. Syncing disks.

  2.2 格式化兩個分區
   第一個格式化成 FAT         mkfs.vfat /dev/sda1
   第二個格式化 格式ext3 格式 mkfs.ext3 /dev/sda2 
 

  1. mkfs.vfat /dev/sda1 #<----------格式化命令
  2. mkfs.vfat 2.11 (12 Mar 2005)
  3. [root@tch u-boot-1.1.6_hxy6410]# mkfs.ext3 /dev/sda2 #<------格式化命令
  4. mke2fs 1.39 (29-May-2006)
  5. Filesystem label=
  6. OS type: Linux
  7. Block size=4096 (log=2)
  8. Fragment size=4096 (log=2)
  9. 242880 inodes, 484825 blocks
  10. 24241 blocks (5.00%) reserved for the super user
  11. First data block=0
  12. Maximum filesystem blocks=499122176
  13. 15 block groups
  14. 32768 blocks per group, 32768 fragments per group
  15. 16192 inodes per group
  16. Superblock backups stored on blocks:
  17. 32768, 98304, 163840, 229376, 294912
  18. Writing inode tables: done
  19. Creating journal (8192 blocks): done
  20. Writing superblocks and filesystem accounting information:
  21. done
  22. This filesystem will be automatically checked every 33 mounts or
  23. 180 days, whichever comes first. Use tune2fs -c or -i to override.

   2.3 測試文件系統是否可以用
          mount -t ext3 /dev/sda2 /mnt

   2.4 把根文件系統拷入SD卡,r表示遞歸,a表示把權限,符號鏈接等信息也拷貝
       cp -ra /home/hxy/rootfs/* /mnt







1.PC機上格式化SD卡
  在桌面的PC機上用SD讀卡器操作SD卡:
 
  一般情況下PC機上第一個U盤整體設備結點是/dev/sda,第二個是 /dev/sdb,在RHEL5下它會被自動mount到/media/disk和 /media/disk_1 目錄.
 
  U盤/dev/sdb上第一個分區是 /dev/sdb1,第二是/dev/sdb2 依此類推.
  根文件系統採用符號鏈接等特性,用FAT32是不行的,這裏直接採用標準的ext3的文件系統.在實測時,把U盤整個做一個分區做ext3根文件系統.總是出不少問題(可能步驟也不對),因此按網上推薦的,做二個分區,第一個分區採用vfat格式,第二個分區才採用ext3的格式.
 
1.1 用fdisk分區
     執行fdisk /dev/sdb
    fdisk有如下常用選項
1. 輸入 m 顯示所有命令列示。 
2. 輸入 p 顯示硬盤分割情形。 
3. 輸入 a 設定硬盤啓動區。 
4. 輸入 n 設定新的硬盤分割區。 
  4.1. 輸入 e 硬盤爲[擴展]分割區(extend)。 
   4.2. 輸入 p 硬盤爲[首要]分割區(primary)。 
5. 輸入 t 改變硬盤分割區屬性。 
6. 輸入 d 刪除硬盤分割區屬性。 
7. 輸入 q 結束不存入硬盤分割區屬性。 
8. 輸入 w 結束並寫入硬盤分割區屬性
    
如果以前U盤有分區,需要輸入d命令來依次刪除分區,以下執行兩次n命令創建一個 400M的FAT分區,以及把剩下的分區設爲ext3,最後用w命令把結果保存下來.

Command (m for help): n

Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1020, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1020, default 1020): +400M

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (202-1020, default 202): 
Using default value 202
Last cylinder or +size or +sizeM or +sizeK (202-1020, default 1020): 
Using default value 1020

Command (m for help): p

Disk /dev/sdb: 2041 MB, 2041577472 bytes
63 heads, 62 sectors/track, 1020 cylinders
Units = cylinders of 3906 * 512 = 1999872 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         201      392522    b  W95 FAT32
/dev/sdb2             202        1020     1599507   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 設備或資源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.

  
1.2 格式化分區
   
    mkfs.vfat /dev/sda1 #第一個分區格式化成fat32
    mkfs.ext3 /dev/sda2 #第二個分區格式化成fat32
  可以用
    tune2fs -l /dev/sda2 來檢驗分區類型 
  
1.3 拷貝根文件系統
    
  mount /dev/sda2 /mnt/sdcard #掛載SD卡
  cp -ra /home/hxy/rootfs/* /mnt/sdcard #把根文件系統拷到SD卡
 
 
2.編譯ARM Linux內核
 
 2.1 修改內核配置
  確保ext3的文件系統支持,和SD卡驅動都被靜態編譯到內核,我使用的是Linux 2.6.29.
 在內核源碼目錄執行 make menuconfig
 
  其中ext3在File system下.成功的配置如下.
  

 <*> Ext3 journalling file system support       
    [*]   Ext3 extended attributes 
    [*]     Ext3 POSIX Access Control Lists 
    [*]     Ext3 Security Labels  
 
SD卡驅動支持在Device Drivers ->MMC/SD/SDIO card support 下,成功的配置如下
 
 --- MMC/SD/SDIO card support                           
 [*]   MMC debugging                                    
 [ ]   Allow unsafe resume (DANGEROUS)                  
       *** MMC/SD/SDIO Card Drivers ***                 
 <*>   MMC block device driver                          
 [*]     Use bounce buffer for simple hosts             
 <*>   SDIO UART/GPS class support                      
 <*>   MMC host test driver                             
       *** MMC/SD/SDIO Host Controller Drivers ***      
 <*>   Secure Digital Host Controller Interface support 
 <*>   Samsung S3C SD/MMC Card Interface support        
  編譯內核 make zImage
 
 2.2 在ARM-linux下的測試.
   
   在一個已經正常啓動的ARM-LINUX下,插入SD卡.出現如下提示,表示SD卡已經安裝上
[root: /]# s3c2440-sdi s3c2440-sdi: running at 0kHz (requested: 0kHz).                            
s3c2440-sdi s3c2440-sdi: running at 198kHz (requested: 197kHz).                                   
s3c2440-sdi s3c2440-sdi: running at 198kHz (requested: 197kHz).                                   
s3c2440-sdi s3c2440-sdi: running at 198kHz (requested: 197kHz).                                   
s3c2440-sdi s3c2440-sdi: running at 198kHz (requested: 197kHz).                                   
s3c2440-sdi s3c2440-sdi: running at 198kHz (requested: 197kHz).                                   
s3c2440-sdi s3c2440-sdi: running at 198kHz (requested: 197kHz).                                   
s3c2440-sdi s3c2440-sdi: running at 198kHz (requested: 197kHz).                                   
s3c2440-sdi s3c2440-sdi: running at 16875kHz (requested: 25000kHz).                               
s3c2440-sdi s3c2440-sdi: running at 16875kHz (requested: 25000kHz).                               
mmc0: new SD card at address 0002                                                                 
mmcblk0: mmc0:0002 00000 1.90 GiB                                                                
 mmcblk0: p1 p2                                                                                   
FAT: utf8 is not a recommended IO charset for FAT filesystems, filesystem will be case sensitive! 
 
但是最後一句在提示FAT有不識別的IO字符集,導致第一個VFAT分區沒有自動創建設備結點.用如下命令只看到mmcblk0p2的結點.(以下操作均是在開發板上進行)
[root: /]# ls -l /dev/mmc*                                             
brw-rw----    1 root     root     179,   0 Sep 22 10:48 /dev/mmcblk0   
brw-rw----    1 root     root     179,   2 Sep 22 10:48 /dev/mmcblk0p2
 
測試ext3分區
mount -t ext3 /dev/mmcblk0p2 /mnt
在/mnt上可以讀寫文件.表示ext3分區正常.
 
 
手動建立第一個分區的結點.
mknod /dev/mmcblk0p1 b 179 1
測試vfat 分區
mount -t vfat /dev/mmcblk0p1 /mnt
在/mnt上可以讀寫文件.表示vfat分區正常.如果想自動創建個設備結點,可以修改/etc/fstat來使用這個分區
 
3.使用SD卡分區作爲根文件
 修改LINUX啓動參數.我用的是u-boot .因此在u-boot shell用set bootargs命令來設置Linux啓動參數.
 成功的啓動參數是 noinitrd root=179:2 rw console=ttySAC0
其中179和2是sd卡上ext3分區的主設備和從設備號.
 
用set bootargs "noinitrd root=179:2 rw console=ttySAC0 "設置後.重啓內核,即可用ext3分區作爲根文件系統
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章