mobile的boot loader之二:bootloadermain函数解析

来源:互联网 发布:grim dawn知乎 编辑:程序博客网 时间:2024/06/11 06:23

      startup.s对底层进行初始化后,调用main()。此函数一般位于main.c下面。main函数调用BLCOMMON.c下的BootloaderMain()函数。函数体如下:

void BootloaderMain (void)
{
      DWORD dwAction;  
      DWORD dwpToc = 0;
      DWORD dwImageStart = 0, dwImageLength = 0, dwLaunchAddr = 0;
      BOOL bDownloaded = FALSE;

      // relocate globals to RAM// 把全局变量定位到ram
      if (!KernelRelocate (pTOC))
      {
             // spin forever     //出错处理
             HALT (BLERR_KERNELRELOCATE);
      }

      // (1) Init debug support. We can use OEMWriteDebugString afterward.
      //始化调试端口,我们就可以使用OEMWriteDebugString 函数
      if (!OEMDebugInit ())
      {
             // spin forever
             HALT (BLERR_DBGINIT);
      }

      // output banner
      EdbgOutputDebugString (NKSignon, CURRENT_VERSION_MAJOR, CURRENT_VERSION_MINOR);

      // (3) initialize platform (clock, drivers, transports, etc)
      //始化平台(时钟,驱动,和传输)
      if (!OEMPlatformInit ())
      {
            // spin forever
            HALT (BLERR_PLATINIT);
       }

       // system ready, preparing for download
       EdbgOutputDebugString ("System ready!/r/nPreparing for download.../r/n");

       // (4) call OEM specific pre-download function
       //用OEM编写的OEMPreDownload函数决定是否下载
       switch (dwAction = OEMPreDownload ())
       {
              case BL_DOWNLOAD:
              // (5) download image
              //下载镜像
              if (!DownloadImage (&dwImageStart, &dwImageLength, &dwLaunchAddr))
              {
                       // error already reported in DownloadImage
                       SPIN_FOREVER;
              }
              bDownloaded = TRUE;

              // Check for pTOC signature ("CECE") here, after image in place
              //检测下载镜像的签名
              if (*(LPDWORD) OEMMapMemAddr (dwImageStart, dwImageStart + ROM_SIGNATURE_OFFSET) == ROM_SIGNATURE)
              {
                      dwpToc = *(LPDWORD) OEMMapMemAddr (dwImageStart, dwImageStart + ROM_SIGNATURE_OFFSET + sizeof(ULONG));
                      // need to map the content again since the pointer is going to be in a fixup address
                      //再一次映射内容
                      dwpToc = (DWORD) OEMMapMemAddr (dwImageStart, dwpToc + g_dwROMOffset);

                      EdbgOutputDebugString ("ROMHDR at Address %Xh/r/n", dwImageStart + ROM_SIGNATURE_OFFSET + sizeof (DWORD));

                      // right after signature
                }

                // fall through
                //没有break,下载后直接继续执行
                case BL_JUMP:
                /*Before jumping to the image, optionally check the image signature.NOTE: if we haven't downloaded the image by now, we assume that it'll be loaded from local storage in OEMLaunch or it already resides in RAM from an earlier download, and in this case, the image start address might be 0. This means  that the image signature routine will need to find the image in storage or in RAM to validate it.  Since the OEM"s OEMLaunch function will need to do this anyways, we trust that it's within their abilities to do it here.*/
               //在启动操作系统之前,再次检查签名,如果没有下载,那么OEMLauch函数应该有能力启动存在本地存储中的镜像
                if (g_bBINDownload && g_pOEMCheckSignature)
                {
                         if (!g_pOEMCheckSignature(dwImageStart, g_dwROMOffset, dwLaunchAddr, bDownloaded))
                                HALT(BLERR_CAT_SIGNATURE);
                 }
                // (5) final call to launch the image. never returned
                //最终启动操作系统镜像,此函数不会返回
                OEMLaunch (dwImageStart, dwImageLength, dwLaunchAddr, (const ROMHDR *)dwpToc);
                // should never return
                //不应该执行到这里
                default:
                       // ERROR! spin forever
                        HALT (BLERR_INVALIDCMD);
         }
}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ymzhou117/archive/2010/04/16/5492313.aspx

原创粉丝点击