分享Syslinux4USB 0.3源码——改自神雕大侠作品

      神雕大侠写的Syslinux4USB是我在无忧论坛里找到的工具,他是2011年写出的,他在帖子里也发布了源码,可惜那个帖子里的链接失效了,我为了这个工具的源码找了2年,终于在Google上搜到了,并且修改了其中的代码,扩展了它的功能。

      神雕的Syslinux4USB用的是syslinux 4.03的版本做的,syslinux 4.03版是2010年发布的,所以算是比较老的版本了,可能对新的硬件不太支持,所以我依次制作了Syslinux4USB 0.2和Syslinux4USB 0.3版本,分别采用syslinux 4.06和syslinux 6.02制作。增加了HDT(Hardware Detection Tool,硬件侦测工具)等一些功能,增加的功能都是从Grml Linux发行版的LiveCD中模仿来的。

      从代码全局来看,主要分为版本模块、界面模块、语言模块、逻辑流程模块、U盘引导模块、文件部署模块等6个部分,我们仅仅需要改的是第一部分和最后一部分,也就是修改一下版本和要复制的文件就可以了。

      首先,从定义的宏VERSION可以看出,这是用来定义版本的,如果你想修改程序名称和版本号,可以修改宏NAME和VERSION的内容。

      然后下面就是修改复制的文件的部分,主要是修改File后的文件路径和名称,Copyfiles后面的文件路径和File后的文件路径对应,而后面的”$BootDir……”内容是用来指定syslinux所有文件放到U盘中的路径,必须逐个指定,File也需要逐个指定。

      由于对NSIS的语法不了解,所以这篇文章只做分享,不做分析,有兴趣的可以翻译NSIS的手册。

      最后,恐怕连神雕本人都想不到这个工具还会有后续版本,本文也算是对前辈的精神的回赠。

      下面是Syslinux4USB 0.3的源代码:

  1 !define NAME "Syslinux4USB"
  2 !define DISTRO "Boot Files"
  3 !define FILENAME "Syslinux4USB"
  4 !define VERSION "v0.3"
  5 !define MUI_ICON "syslinux4usb.ico"
  6 RequestExecutionLevel highest
  7 SetCompressor LZMA
  8 CRCCheck On
  9 XPStyle on
 10 ShowInstDetails show
 11 BrandingText "USB启动界面美化最佳方案 神雕teasiu作品"
 12 CompletedText "启动安装完成,您的USB盘现在已经准备好启动了!"
 13 InstallButtonText "创建"
 14 Name "${NAME} ${VERSION}"
 15 OutFile "${FILENAME}.exe"
 16 !include "MUI2.nsh"
 17 !include "FileFunc.nsh"
 18 ; Interface settings
 19 !define MUI_FINISHPAGE_NOAUTOCLOSE
 20 !define MUI_HEADERIMAGE
 21 !define MUI_HEADERIMAGE_BITMAP "usb-logo-nsis.bmp"
 22 !define MUI_HEADERIMAGE_BITMAP_NOSTRETCH
 23 !define MUI_HEADERIMAGE_RIGHT
 24 ; License page
 25 !define MUI_TEXT_LICENSE_SUBTITLE $(License_Subtitle)
 26 !define MUI_LICENSEPAGE_TEXT_TOP $(License_Text_Top)
 27 !define MUI_LICENSEPAGE_TEXT_BOTTOM $(License_Text_Bottom)
 28 !insertmacro MUI_PAGE_LICENSE "license.txt"
 29 ; Drive page
 30 Var DestDriveTxt
 31 Var DestDrive
 32 Var DestDisk
 33 Var LabelDrivePageText
 34 Var LabelDriveSelect
 35 Var Format
 36 Var FormatMe
 37 Var Warning
 38 Page custom drivePage
 39 ; Instfiles page
 40 !define MUI_INSTFILESPAGE_COLORS "00FF00 000000" ;Green and Black
 41 !define MUI_TEXT_INSTALLING_TITLE $(Install_Title)
 42 !define MUI_TEXT_INSTALLING_SUBTITLE $(Install_SubTitle)
 43 !define MUI_TEXT_FINISH_SUBTITLE $(Install_Finish_Sucess)
 44 !insertmacro MUI_PAGE_INSTFILES
 45 ; Finish page
 46 !define MUI_FINISHPAGE_TITLE $(Finish_Title)
 47 !define MUI_FINISHPAGE_TEXT $(Finish_Text)
 48 !define MUI_FINISHPAGE_LINK $(Finish_Link)
 49 !define MUI_FINISHPAGE_LINK_LOCATION "http://bbs.wuyou.net/"
 50 !insertmacro MUI_PAGE_FINISH
 51 ; Language files
 52 !insertmacro MUI_LANGUAGE "English" ;first language is the default language
 53 !insertmacro MUI_LANGUAGE "SimpChinese"
 54 ; English
 55 LangString License_Subtitle ${LANG_ENGLISH} "Please review the license terms before running ${NAME}."
 56 LangString License_Text_Top ${LANG_ENGLISH} "The software within this program falls under the following License."
 57 LangString License_Text_Bottom ${LANG_ENGLISH} "You must accept the terms of this License agreement to run ${NAME}. If you agree, Click I Agree to Continue."
 58 LangString Finish_Title ${LANG_ENGLISH} "${NAME} has completed the installation."
 59 LangString Finish_Text ${LANG_ENGLISH} "The necessary files are installed on your USB drive and the drive is bootable.$\r$\n$\r$\nNow you should proceed to see if it boots"
 60 LangString Finish_Link ${LANG_ENGLISH} "Visit the Official Memtest86+ Site"
 61 LangString DrivePage_Title ${LANG_ENGLISH} "Choose USB drive location"
 62 LangString DrivePage_Title2 ${LANG_ENGLISH} "Choose the USB drive in which to make bootable."
 63 LangString DrivePage_Text ${LANG_ENGLISH} "Please select your USB Flash Drive and Format option. ${NAME} will proceed to make this drive Bootable and install the Memtest86+ tool on it."
 64 LangString DrivePage_Input ${LANG_ENGLISH} "Select your USB Flash Drive"
 65 LangString WarningPage_Text ${LANG_ENGLISH} "WARNING!! Contents on this Drive will be deleted. Any exisiting MBR WILL be overwritten. Make sure you have backed up any important content!$\r$\n$\r$\nWhen you are sure, click Create to proceed."
 66 LangString Syslinux_Creation ${LANG_ENGLISH} "Create syslinux configuration and moving files to $DestDrive "
 67 LangString Syslinux_Execution ${LANG_ENGLISH} "Execute syslinux on $R0"
 68 LangString Syslinux_Warning ${LANG_ENGLISH} "An error ($R8) occurred while executing syslinux.$\r$\nYour USB drive won't be bootable..."
 69 LangString Install_Title ${LANG_ENGLISH} "Installing ${DISTRO}"
 70 LangString Install_SubTitle ${LANG_ENGLISH} "Please wait while ${NAME} installs ${DISTRO} on $0"
 71 LangString Install_Finish_Sucess ${LANG_ENGLISH} "${NAME} sucessfully installed ${DISTRO} on $0"
 72 LangString License_Subtitle ${LANG_SIMPCHINESE} "创建USB启动之前请认真阅读本协议 ${NAME}."
 73 LangString License_Text_Top ${LANG_SIMPCHINESE} "本程序的软件属于下面的许可."
 74 LangString License_Text_Bottom ${LANG_SIMPCHINESE} "您必须接受以下许可协议来运行 ${NAME}.$\r$\n$\r$\n 如果你同意,请按 我接受 然后继续."
 75 LangString Finish_Title ${LANG_SIMPCHINESE} "${NAME} 已经完成启动创建."
 76 LangString Finish_Text ${LANG_SIMPCHINESE} "必要的文件已安装在您的USB驱动器并且可启动.$\r$\n$\r$\n现在您可以启动它并进行引导了."
 77 LangString Finish_Link ${LANG_SIMPCHINESE} "访问本启动器作者的官方网站"
 78 LangString DrivePage_Title ${LANG_SIMPCHINESE} "选择USB驱动器的位置"
 79 LangString DrivePage_Title2 ${LANG_SIMPCHINESE} "在框中选择您想让它可启动的USB驱动器."
 80 LangString DrivePage_Text ${LANG_SIMPCHINESE} "请选择您的USB闪存驱动器和格式选项. ${NAME}使该驱动器可启动并进行引导."
 81 LangString DrivePage_Input ${LANG_SIMPCHINESE} "选择您的USB闪存驱动器"
 82 LangString WarningPage_Text ${LANG_SIMPCHINESE} "警告!此驱动器的内容将被删除. 任何现有的MBR将被覆盖. 请确认您的重要内容已经备份!$\r$\n$\r$\n如果您确认完毕, 点击创建按钮."
 83 LangString Syslinux_Creation ${LANG_SIMPCHINESE} "创建syslinux的配置和移动文件到$DestDrive"
 84 LangString Syslinux_Execution ${LANG_SIMPCHINESE} "执行syslinux到 $R0"
 85 LangString Syslinux_Warning ${LANG_SIMPCHINESE} "一个错误 ($R8) 发生在当执行syslinux时.$\r$\n您的USB驱动器将不可启动.."
 86 LangString Install_Title ${LANG_SIMPCHINESE} "安装中 ${DISTRO}"
 87 LangString Install_SubTitle ${LANG_SIMPCHINESE} "请稍后 ${NAME} 安装 ${DISTRO} 到 $0"
 88 LangString Install_Finish_Sucess ${LANG_SIMPCHINESE} "${NAME} 已经成功安装了 ${DISTRO} 到 $0"
 89 Function drivePage
 90 !insertmacro MUI_HEADER_TEXT $(DrivePage_Title) $(DrivePage_Title2)
 91 nsDialogs::Create 1018
 92 ${If} $DestDrive == ""
 93 GetDlgItem $6 $HWNDPARENT 1 ; Get next control handle
 94 EnableWindow $6 0 ; disable next
 95 ${EndIf}
 96 ${NSD_CreateLabel} 0 0 100% 30 $(DrivePage_Text)
 97 Pop $LabelDrivePageText
 98 ${NSD_CreateLabel} 0 50 100% 15 $(DrivePage_Input)
 99 Pop $LabelDriveSelect
100 ${NSD_CreateDroplist} 0 68 23% 20 ""
101 Pop $DestDriveTxt
102 ${NSD_OnChange} $DestDriveTxt db_select.onchange
103 ${GetDrives} "FDD" driveList
104 ${If} $DestDrive != ""
105 ${NSD_CB_SelectString} $DestDriveTxt $DestDrive
106 ${EndIf}
107 ; Format Drive Option
108 ${NSD_CreateCheckBox} 25% 68 75% 15 "如果你想格式化USB驱动器,选中此复选框."
109 Pop $Format
110 ${NSD_OnClick} $Format FormatIt
111 ; Warning Label
112 ${NSD_CreateLabel} 0 120 100% 60 $(WarningPage_Text)
113 Pop $Warning
114 EnableWindow $Format 0
115 ShowWindow $Warning 0
116 GetDlgItem $6 $HWNDPARENT 3
117 ShowWindow $6 0 ; Disable Back
118 nsDialogs::Show
119 FunctionEnd
120 Function db_select.onchange
121 Pop $DestDriveTxt
122 ${NSD_GetText} $DestDriveTxt $0
123 StrCpy $DestDrive "$0"
124 StrCpy $DestDisk $DestDrive -1
125 GetDlgItem $6 $HWNDPARENT 1 ; Get "Next" control handle
126 EnableWindow $6 1 ; enable "Next" control
127 EnableWindow $Format 1
128 ShowWindow $Warning 1
129 SetCtlColors $Warning /Branding FF0000
130 Call FormatIt
131 FunctionEnd
132 Function driveList
133 SendMessage $DestDriveTxt ${CB_ADDSTRING} 0 "STR:$9"
134 Push 1
135 FunctionEnd
136 Function FormatIt ; Set Format Option
137 ${NSD_GetState} $Format $FormatMe
138 ${If} $FormatMe == ${BST_CHECKED}
139 ${NSD_Check} $Format
140 StrCpy $FormatMe "Yes"
141 ${NSD_SetText} $Format "我们将格式化 $DestDrive 为 Fat32."
142 ${ElseIf} $FormatMe == ${BST_UNCHECKED}
143 ${NSD_Uncheck} $Format
144 ${NSD_SetText} $Format "(推荐)如果你想格式化USB驱动器,选中此复选框 $DestDrive"
145 ${EndIf}
146 FunctionEnd
147 Function FormatYes ; If Format is checked, do something
148 ${If} $FormatMe == "Yes"
149 DetailPrint "正在格式化 $DestDisk"
150 InitPluginsDir
151 File /oname=$PLUGINSDIR\fbinst.exe "fbinst.exe"
152 # nsExec::ExecToLog '"cmd" /c "format $DestDisk /FS:Fat32 /V:JHJHORG /Q /y"'
153 nsExec::ExecToLog '"cmd" /c "echo y|$PLUGINSDIR\fbinst $DestDisk format --raw --force --fat32"'
154 ${EndIf}
155 FunctionEnd
156 Section "Copyfiles" main
157 Call FormatYes ; Format the Drive?
158 InitPluginsDir
159 File /oname=$PLUGINSDIR\syslinux.exe "syslinux.exe"
160 File /oname=$PLUGINSDIR\syslinux.cfg "syslinux.cfg"
161 File /oname=$PLUGINSDIR\Readme.txt "Readme.txt"
162 File /oname=$PLUGINSDIR\back.jpg "back.jpg"
163 File /oname=$PLUGINSDIR\balder10.imz "balder10.imz"
164 File /oname=$PLUGINSDIR\chain.c32 "chain.c32"
165 File /oname=$PLUGINSDIR\gfxboot.c32 "gfxboot.c32"
166 File /oname=$PLUGINSDIR\hdt.c32 "hdt.c32"
167 File /oname=$PLUGINSDIR\ifcpu64.c32 "ifcpu64.c32"
168 File /oname=$PLUGINSDIR\ipxe.lkrn "ipxe.lkrn"
169 File /oname=$PLUGINSDIR\libcom32.c32 "libcom32.c32"
170 File /oname=$PLUGINSDIR\libgpl.c32 "libgpl.c32"
171 File /oname=$PLUGINSDIR\libmenu.c32 "libmenu.c32"
172 File /oname=$PLUGINSDIR\libutil.c32 "libutil.c32"
173 File /oname=$PLUGINSDIR\mboot.c32 "mboot.c32"
174 File /oname=$PLUGINSDIR\memdisk "memdisk"
175 File /oname=$PLUGINSDIR\menu.c32 "menu.c32"
176 File /oname=$PLUGINSDIR\pci.ids "pci.ids"
177 File /oname=$PLUGINSDIR\poweroff.c32 "poweroff.c32"
178 File /oname=$PLUGINSDIR\reboot.c32 "reboot.c32"
179 File /oname=$PLUGINSDIR\vesamenu.c32 "vesamenu.c32"
180 SetShellVarContext all
181 ; Execute syslinux on the drive
182 StrCpy $R0 $DestDrive -1 ; remove \ for syslinux
183 ClearErrors
184 DetailPrint $(Syslinux_Execution)
185 ExecWait '$PLUGINSDIR\syslinux.exe -maf $R0' $R8
186 DetailPrint "Return $R8"
187 Banner::destroy
188 ${If} $R8 != 0
189 MessageBox MB_ICONEXCLAMATION|MB_OK $(Syslinux_Warning)
190 ${EndIf}
191 ; Create files
192 Var /GLOBAL BootDir
193 StrCpy $BootDir $DestDrive -1
194 StrCpy $BootDir "$BootDir"
195 DetailPrint $(Syslinux_Creation)
196 CreateDirectory $BootDir\boot
197 CopyFiles "$PLUGINSDIR\syslinux.cfg" "$BootDir\syslinux.cfg" ; move syslinux.cfg
198 CopyFiles "$PLUGINSDIR\Readme.txt" "$BootDir\Readme.txt" ; move Readme
199 CopyFiles "$PLUGINSDIR\libcom32.c32" "$BootDir\libcom32.c32"
200 CopyFiles "$PLUGINSDIR\libgpl.c32" "$BootDir\libgpl.c32"
201 CopyFiles "$PLUGINSDIR\libmenu.c32" "$BootDir\libmenu.c32"
202 CopyFiles "$PLUGINSDIR\libutil.c32" "$BootDir\libutil.c32"
203 CopyFiles "$PLUGINSDIR\back.jpg" "$BootDir\boot\back.jpg"
204 CopyFiles "$PLUGINSDIR\balder10.imz" "$BootDir\boot\balder10.imz"
205 CopyFiles "$PLUGINSDIR\chain.c32" "$BootDir\boot\chain.c32"
206 CopyFiles "$PLUGINSDIR\gfxboot.c32" "$BootDir\boot\gfxboot.c32"
207 CopyFiles "$PLUGINSDIR\hdt.c32" "$BootDir\boot\hdt.c32"
208 CopyFiles "$PLUGINSDIR\ifcpu64.c32" "$BootDir\boot\ifcpu64.c32"
209 CopyFiles "$PLUGINSDIR\ipxe.lkrn" "$BootDir\boot\ipxe.lkrn"
210 CopyFiles "$PLUGINSDIR\mboot.c32" "$BootDir\boot\mboot.c32"
211 CopyFiles "$PLUGINSDIR\memdisk" "$BootDir\boot\memdisk"
212 CopyFiles "$PLUGINSDIR\menu.c32" "$BootDir\boot\menu.c32"
213 CopyFiles "$PLUGINSDIR\pci.ids" "$BootDir\boot\pci.ids"
214 CopyFiles "$PLUGINSDIR\poweroff.c32" "$BootDir\boot\poweroff.c32"
215 CopyFiles "$PLUGINSDIR\reboot.c32" "$BootDir\boot\reboot.c32"
216 CopyFiles "$PLUGINSDIR\vesamenu.c32" "$BootDir\boot\vesamenu.c32"
217 SectionEnd

 

posted @ 2014-04-18 11:38  MIX实验室  阅读(948)  评论(0编辑  收藏  举报