reactos操作系统实现(64)

前端之家收集整理的这篇文章主要介绍了reactos操作系统实现(64)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_301_2@在安装一个操作系统时,绝大多数都是把引导程序安装在硬盘里,下面就来了解安装到硬盘里具体过程,实现代码如下:

@H_301_2@#001 static PAGE_NUMBER

@H_301_2@#002 BootLoaderHarddiskPage(PINPUT_RECORD Ir)

@H_301_2@#003 {

@H_301_2@#004 UCHAR PartitionType;

@H_301_2@#005 NTSTATUS Status;

@H_301_2@#006

@H_301_2@

@H_301_2@获取硬盘分区的类型,然后判断是否可以安装引导程序。

@H_301_2@#007 PartitionType = PartitionList->ActiveBootPartition->PartInfo[0].PartitionType;

@H_301_2@#008 if ((PartitionType == PARTITION_FAT_12) ||

@H_301_2@#009 (PartitionType == PARTITION_FAT_16) ||

@H_301_2@#010 (PartitionType == PARTITION_HUGE) ||

@H_301_2@#011 (PartitionType == PARTITION_XINT13) ||

@H_301_2@#012 (PartitionType == PARTITION_FAT32) ||

@H_301_2@#013 (PartitionType == PARTITION_FAT32_XINT13))

@H_301_2@#014 {

@H_301_2@

@H_301_2@可以安装引导程序,调用函数InstallFatBootcodeToPartition实现。

@H_301_2@#015 Status = InstallFatBootcodeToPartition(&SystemRootPath,

@H_301_2@#016 &SourceRootPath,

@H_301_2@#017 &DestinationArcPath,

@H_301_2@#018 PartitionType);

@H_301_2@#019 if (!NT_SUCCESS(Status))

@H_301_2@#020 {

@H_301_2@#021 MUIDisplayError(ERROR_INSTALL_BOOTCODE,Ir,POPUP_WAIT_ENTER);

@H_301_2@#022 return QUIT_PAGE;

@H_301_2@#023 }

@H_301_2@#024

@H_301_2@#025 return SUCCESS_PAGE;

@H_301_2@#026 }

@H_301_2@#027 else

@H_301_2@#028 {

@H_301_2@

@H_301_2@如果不能安装,就提示出错。

@H_301_2@#029 MUIDisplayError(ERROR_WRITE_BOOT,POPUP_WAIT_ENTER);

@H_301_2@#030 return QUIT_PAGE;

@H_301_2@#031 }

@H_301_2@#032

@H_301_2@#033 return BOOT_LOADER_HARDDISK_PAGE;

@H_301_2@#034 }

@H_301_2@

@H_301_2@下面继续分析函数InstallFatBootcodeToPartition的实现,如下:

@H_301_2@#001 NTSTATUS

@H_301_2@#002 InstallFatBootcodeToPartition(PUNICODE_STRING SystemRootPath,

@H_301_2@#003 PUNICODE_STRING SourceRootPath,

@H_301_2@#004 PUNICODE_STRING DestinationArcPath,

@H_301_2@#005 UCHAR PartitionType)

@H_301_2@#006 {

@H_301_2@#007 #ifdef __REACTOS__

@H_301_2@#008 WCHAR SrcPath[MAX_PATH];

@H_301_2@#009 WCHAR DstPath[MAX_PATH];

@H_301_2@#010 NTSTATUS Status;

@H_301_2@#011

@H_301_2@#012 /* FAT or FAT32 partition */

@H_301_2@#013 DPRINT("System path: '%wZ'/n",SystemRootPath);

@H_301_2@#014

@H_301_2@

@H_301_2@判断分区路径里是否存在ntldrboot.ini文件

@H_301_2@#015 if (DoesFileExist(SystemRootPath->Buffer,L"ntldr") == TRUE ||

@H_301_2@#016 DoesFileExist(SystemRootPath->Buffer,L"boot.ini") == TRUE)

@H_301_2@#017 {

@H_301_2@

@H_301_2@如果发现NT2000XP的引导程序,就只需要设置选项,让ntldr来加freeldr.sys程序就行了。

@H_301_2@#018 /* Search root directory for 'ntldr' and 'boot.ini'. */

@H_301_2@#019 DPRINT("Found Microsoft Windows NT/2000/XP boot loader/n");

@H_301_2@#020

@H_301_2@#021 /* Copy FreeLoader to the boot partition */

@H_301_2@#022 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#023 wcscat(SrcPath,L"//loader//freeldr.sys");

@H_301_2@#024 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#025 wcscat(DstPath,L"//freeldr.sys");

@H_301_2@#026

@H_301_2@

@H_301_2@这里开始拷贝文件

@H_301_2@#027 DPRINT("Copy: %S ==> %S/n",SrcPath,DstPath);

@H_301_2@#028 Status = SetupCopyFile(SrcPath,DstPath);

@H_301_2@#029 if (!NT_SUCCESS(Status))

@H_301_2@#030 {

@H_301_2@#031 DPRINT1("SetupCopyFile() Failed (Status %lx)/n",Status);

@H_301_2@#032 return Status;

@H_301_2@#033 }

@H_301_2@#034

@H_301_2@

@H_301_2@更新freeldr.ini文件

@H_301_2@#035 /* Create or update freeldr.ini */

@H_301_2@#036 if (DoesFileExist(SystemRootPath->Buffer,L"freeldr.ini") == FALSE)

@H_301_2@#037 {

@H_301_2@#038 /* Create new 'freeldr.ini' */

@H_301_2@#039 DPRINT1("Create new 'freeldr.ini'/n");

@H_301_2@#040 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#041 wcscat(DstPath,L"//freeldr.ini");

@H_301_2@#042

@H_301_2@#043 Status = CreateFreeLoaderIniForReactos(DstPath,

@H_301_2@#044 DestinationArcPath->Buffer);

@H_301_2@#045 if (!NT_SUCCESS(Status))

@H_301_2@#046 {

@H_301_2@#047 DPRINT1("CreateFreeLoaderIniForReactos() Failed (Status %lx)/n",Status);

@H_301_2@#048 return Status;

@H_301_2@#049 }

@H_301_2@#050

@H_301_2@

@H_301_2@安装新的引导代码到引导扇区。

@H_301_2@#051 /* Install new bootcode */

@H_301_2@#052 if (PartitionType == PARTITION_FAT32 ||

@H_301_2@#053 PartitionType == PARTITION_FAT32_XINT13)

@H_301_2@#054 {

@H_301_2@#055 /* Install FAT32 bootcode */

@H_301_2@#056 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#057 wcscat(SrcPath,L"//loader//fat32.bin");

@H_301_2@#058 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#059 wcscat(DstPath,L"//bootsect.ros");

@H_301_2@#060

@H_301_2@#061 DPRINT1("Install FAT32 bootcode: %S ==> %S/n",DstPath);

@H_301_2@#062 Status = InstallFat32BootCodeToFile(SrcPath,

@H_301_2@#063 DstPath,

@H_301_2@#064 SystemRootPath->Buffer);

@H_301_2@#065 if (!NT_SUCCESS(Status))

@H_301_2@#066 {

@H_301_2@#067 DPRINT1("InstallFat32BootCodeToFile() Failed (Status %lx)/n",Status);

@H_301_2@#068 return Status;

@H_301_2@#069 }

@H_301_2@#070 }

@H_301_2@#071 else

@H_301_2@#072 {

@H_301_2@#073 /* Install FAT16 bootcode */

@H_301_2@#074 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#075 wcscat(SrcPath,L"//loader//fat.bin");

@H_301_2@#076 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#077 wcscat(DstPath,L"//bootsect.ros");

@H_301_2@#078

@H_301_2@#079 DPRINT1("Install FAT bootcode: %S ==> %S/n",DstPath);

@H_301_2@#080 Status = InstallFat16BootCodeToFile(SrcPath,

@H_301_2@#081 DstPath,

@H_301_2@#082 SystemRootPath->Buffer);

@H_301_2@#083 if (!NT_SUCCESS(Status))

@H_301_2@#084 {

@H_301_2@#085 DPRINT1("InstallFat16BootCodeToFile() Failed (Status %lx)/n",Status);

@H_301_2@#086 return Status;

@H_301_2@#087 }

@H_301_2@#088 }

@H_301_2@#089 }

@H_301_2@#090 else

@H_301_2@#091 {

@H_301_2@#092 /* Update existing 'freeldr.ini' */

@H_301_2@#093 DPRINT1("Update existing 'freeldr.ini'/n");

@H_301_2@#094 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#095 wcscat(DstPath,L"//freeldr.ini");

@H_301_2@#096

@H_301_2@#097 Status = UpdateFreeLoaderIni(DstPath,

@H_301_2@#098 DestinationArcPath->Buffer);

@H_301_2@#099 if (!NT_SUCCESS(Status))

@H_301_2@#100 {

@H_301_2@#101 DPRINT1("UpdateFreeLoaderIni() Failed (Status %lx)/n",Status);

@H_301_2@#102 return Status;

@H_301_2@#103 }

@H_301_2@#104 }

@H_301_2@#105

@H_301_2@#106 /* Update 'boot.ini' */

@H_301_2@#107 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#108 wcscat(DstPath,L"//boot.ini");

@H_301_2@#109

@H_301_2@#110 DPRINT1("Update 'boot.ini': %S/n",DstPath);

@H_301_2@#111 Status = UpdateBootIni(DstPath,

@H_301_2@#112 L"C://bootsect.ros",

@H_301_2@#113 L"/"ReactOS/"");

@H_301_2@#114 if (!NT_SUCCESS(Status))

@H_301_2@#115 {

@H_301_2@#116 DPRINT1("UpdateBootIni() Failed (Status %lx)/n",Status);

@H_301_2@#117 return Status;

@H_301_2@#118 }

@H_301_2@#119 }

@H_301_2@#120 else if (DoesFileExist(SystemRootPath->Buffer,L"io.sys") == TRUE ||

@H_301_2@#121 DoesFileExist(SystemRootPath->Buffer,L"msdos.sys") == TRUE)

@H_301_2@#122 {

@H_301_2@

@H_301_2@查找分区里是否有DOS操作系统。

@H_301_2@#123 /* Search for root directory for 'io.sys' and 'msdos.sys'. */

@H_301_2@#124 DPRINT1("Found Microsoft DOS or Windows 9x boot loader/n");

@H_301_2@#125

@H_301_2@#126 /* Copy FreeLoader to the boot partition */

@H_301_2@#127 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#128 wcscat(SrcPath,L"//loader//freeldr.sys");

@H_301_2@#129 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#130 wcscat(DstPath,L"//freeldr.sys");

@H_301_2@#131

@H_301_2@

@H_301_2@拷贝文件

@H_301_2@#132 DPRINT("Copy: %S ==> %S/n",DstPath);

@H_301_2@#133 Status = SetupCopyFile(SrcPath,DstPath);

@H_301_2@#134 if (!NT_SUCCESS(Status))

@H_301_2@#135 {

@H_301_2@#136 DPRINT1("SetupCopyFile() Failed (Status %lx)/n",Status);

@H_301_2@#137 return Status;

@H_301_2@#138 }

@H_301_2@#139

@H_301_2@

@H_301_2@创建并更新freeldr.ini文件

@H_301_2@#140 /* Create or update 'freeldr.ini' */

@H_301_2@#141 if (DoesFileExist(SystemRootPath->Buffer,L"freeldr.ini") == FALSE)

@H_301_2@#142 {

@H_301_2@#143 /* Create new 'freeldr.ini' */

@H_301_2@#144 DPRINT1("Create new 'freeldr.ini'/n");

@H_301_2@#145 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#146 wcscat(DstPath,L"//freeldr.ini");

@H_301_2@#147

@H_301_2@#148 Status = CreateFreeLoaderIniForDos(DstPath,

@H_301_2@#149 DestinationArcPath->Buffer);

@H_301_2@#150 if (!NT_SUCCESS(Status))

@H_301_2@#151 {

@H_301_2@#152 DPRINT1("CreateFreeLoaderIniForDos() Failed (Status %lx)/n",Status);

@H_301_2@#153 return Status;

@H_301_2@#154 }

@H_301_2@#155

@H_301_2@#156 /* Save current bootsector as 'BOOTSECT.DOS' */

@H_301_2@#157 wcscpy(SrcPath,SystemRootPath->Buffer);

@H_301_2@#158 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#159 wcscat(DstPath,L"//bootsect.dos");

@H_301_2@#160

@H_301_2@#161 DPRINT1("Save bootsector: %S ==> %S/n",DstPath);

@H_301_2@#162 Status = SaveCurrentBootSector(SrcPath,

@H_301_2@#163 DstPath);

@H_301_2@#164 if (!NT_SUCCESS(Status))

@H_301_2@#165 {

@H_301_2@#166 DPRINT1("SaveCurrentBootSector() Failed (Status %lx)/n",Status);

@H_301_2@#167 return Status;

@H_301_2@#168 }

@H_301_2@#169

@H_301_2@#170 /* Install new bootsector */

@H_301_2@#171 if (PartitionType == PARTITION_FAT32 ||

@H_301_2@#172 PartitionType == PARTITION_FAT32_XINT13)

@H_301_2@#173 {

@H_301_2@#174 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#175 wcscat(SrcPath,L"//loader//fat32.bin");

@H_301_2@#176

@H_301_2@#177 DPRINT1("Install FAT32 bootcode: %S ==> %S/n",SystemRootPath->Buffer);

@H_301_2@#178 Status = InstallFat32BootCodeToDisk(SrcPath,

@H_301_2@#179 SystemRootPath->Buffer);

@H_301_2@#180 if (!NT_SUCCESS(Status))

@H_301_2@#181 {

@H_301_2@#182 DPRINT1("InstallFat32BootCodeToDisk() Failed (Status %lx)/n",Status);

@H_301_2@#183 return Status;

@H_301_2@#184 }

@H_301_2@#185 }

@H_301_2@#186 else

@H_301_2@#187 {

@H_301_2@#188 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#189 wcscat(SrcPath,L"//loader//fat.bin");

@H_301_2@#190

@H_301_2@#191 DPRINT1("Install FAT bootcode: %S ==> %S/n",SystemRootPath->Buffer);

@H_301_2@#192 Status = InstallFat16BootCodeToDisk(SrcPath,

@H_301_2@#193 SystemRootPath->Buffer);

@H_301_2@#194 if (!NT_SUCCESS(Status))

@H_301_2@#195 {

@H_301_2@#196 DPRINT1("InstallFat16BootCodeToDisk() Failed (Status %lx)/n",Status);

@H_301_2@#197 return Status;

@H_301_2@#198 }

@H_301_2@#199 }

@H_301_2@#200 }

@H_301_2@#201 else

@H_301_2@#202 {

@H_301_2@#203 /* Update existing 'freeldr.ini' */

@H_301_2@#204 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#205 wcscat(DstPath,L"//freeldr.ini");

@H_301_2@#206

@H_301_2@#207 Status = UpdateFreeLoaderIni(DstPath,

@H_301_2@#208 DestinationArcPath->Buffer);

@H_301_2@#209 if (!NT_SUCCESS(Status))

@H_301_2@#210 {

@H_301_2@#211 DPRINT1("UpdateFreeLoaderIni() Failed (Status %lx)/n",Status);

@H_301_2@#212 return Status;

@H_301_2@#213 }

@H_301_2@#214 }

@H_301_2@#215 }

@H_301_2@#216 else

@H_301_2@#217 {

@H_301_2@

@H_301_2@这个硬盘分区没有任何已经安装的系统。

@H_301_2@#218 /* No or unknown boot loader */

@H_301_2@#219 DPRINT1("No or unknown boot loader found/n");

@H_301_2@#220

@H_301_2@

@H_301_2@拷贝Reactos的引导程序和配置文件

@H_301_2@#221 /* Copy FreeLoader to the boot partition */

@H_301_2@#222 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#223 wcscat(SrcPath,L"//loader//freeldr.sys");

@H_301_2@#224 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#225 wcscat(DstPath,L"//freeldr.sys");

@H_301_2@#226

@H_301_2@#227 DPRINT("Copy: %S ==> %S/n",DstPath);

@H_301_2@#228 Status = SetupCopyFile(SrcPath,DstPath);

@H_301_2@#229 if (!NT_SUCCESS(Status))

@H_301_2@#230 {

@H_301_2@#231 DPRINT1("SetupCopyFile() Failed (Status %lx)/n",Status);

@H_301_2@#232 return Status;

@H_301_2@#233 }

@H_301_2@#234

@H_301_2@

@H_301_2@创建和更新引导配置freeldr.ini文件

@H_301_2@#235 /* Create or update 'freeldr.ini' */

@H_301_2@#236 if (DoesFileExist(SystemRootPath->Buffer,L"freeldr.ini") == FALSE)

@H_301_2@#237 {

@H_301_2@#238 /* Create new freeldr.ini */

@H_301_2@#239 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#240 wcscat(DstPath,L"//freeldr.ini");

@H_301_2@#241

@H_301_2@#242 DPRINT("Copy: %S ==> %S/n",DstPath);

@H_301_2@#243 Status = CreateFreeLoaderIniForReactos(DstPath,

@H_301_2@#244 DestinationArcPath->Buffer);

@H_301_2@#245 if (!NT_SUCCESS(Status))

@H_301_2@#246 {

@H_301_2@#247 DPRINT1("CreateFreeLoaderIniForReactos() Failed (Status %lx)/n",Status);

@H_301_2@#248 return Status;

@H_301_2@#249 }

@H_301_2@#250

@H_301_2@

@H_301_2@保存当前引导扇区代码BOOTSECT.OLD

@H_301_2@#251 /* Save current bootsector as 'BOOTSECT.OLD' */

@H_301_2@#252 wcscpy(SrcPath,SystemRootPath->Buffer);

@H_301_2@#253 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#254 wcscat(DstPath,L"//bootsect.old");

@H_301_2@#255

@H_301_2@#256 DPRINT("Save bootsector: %S ==> %S/n",DstPath);

@H_301_2@#257 Status = SaveCurrentBootSector(SrcPath,

@H_301_2@#258 DstPath);

@H_301_2@#259 if (!NT_SUCCESS(Status))

@H_301_2@#260 {

@H_301_2@#261 DPRINT1("SaveCurrentBootSector() Failed (Status %lx)/n",Status);

@H_301_2@#262 return Status;

@H_301_2@#263 }

@H_301_2@#264

@H_301_2@

@H_301_2@安装新的引导扇区代码硬盘分区。

@H_301_2@#265 /* Install new bootsector */

@H_301_2@#266 if (PartitionType == PARTITION_FAT32 ||

@H_301_2@#267 PartitionType == PARTITION_FAT32_XINT13)

@H_301_2@#268 {

@H_301_2@#269 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#270 wcscat(SrcPath,L"//loader//fat32.bin");

@H_301_2@#271

@H_301_2@#272 DPRINT("Install FAT32 bootcode: %S ==> %S/n",SystemRootPath->Buffer);

@H_301_2@#273 Status = InstallFat32BootCodeToDisk(SrcPath,

@H_301_2@#274 SystemRootPath->Buffer);

@H_301_2@#275 if (!NT_SUCCESS(Status))

@H_301_2@#276 {

@H_301_2@#277 DPRINT1("InstallFat32BootCodeToDisk() Failed (Status %lx)/n",Status);

@H_301_2@#278 return Status;

@H_301_2@#279 }

@H_301_2@#280 }

@H_301_2@#281 else

@H_301_2@#282 {

@H_301_2@#283 wcscpy(SrcPath,SourceRootPath->Buffer);

@H_301_2@#284 wcscat(SrcPath,L"//loader//fat.bin");

@H_301_2@#285

@H_301_2@#286 DPRINT("Install FAT bootcode: %S ==> %S/n",SystemRootPath->Buffer);

@H_301_2@#287 Status = InstallFat16BootCodeToDisk(SrcPath,

@H_301_2@#288 SystemRootPath->Buffer);

@H_301_2@#289 if (!NT_SUCCESS(Status))

@H_301_2@#290 {

@H_301_2@#291 DPRINT1("InstallFat16BootCodeToDisk() Failed (Status %lx)/n",Status);

@H_301_2@#292 return Status;

@H_301_2@#293 }

@H_301_2@#294 }

@H_301_2@#295 }

@H_301_2@#296 else

@H_301_2@#297 {

@H_301_2@#298 /* Update existing 'freeldr.ini' */

@H_301_2@#299 wcscpy(DstPath,SystemRootPath->Buffer);

@H_301_2@#300 wcscat(DstPath,L"//freeldr.ini");

@H_301_2@#301

@H_301_2@#302 Status = UpdateFreeLoaderIni(DstPath,

@H_301_2@#303 DestinationArcPath->Buffer);

@H_301_2@#304 if (!NT_SUCCESS(Status))

@H_301_2@#305 {

@H_301_2@#306 DPRINT1("UpdateFreeLoaderIni() Failed (Status %lx)/n",Status);

@H_301_2@#307 return Status;

@H_301_2@#308 }

@H_301_2@#309 }

@H_301_2@#310 }

@H_301_2@#311

@H_301_2@#312 return STATUS_SUCCESS;

@H_301_2@#313 #else

@H_301_2@#314 return STATUS_NOT_IMPLEMENTED;

@H_301_2@#315 #endif

@H_301_2@#316}

猜你在找的React相关文章