⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comeml.c

📁 The Lite Evaluation/Demonstration Kit is intended to illustrate use of the AN3042. The AN3042 is c
💻 C
📖 第 1 页 / 共 4 页
字号:
   // Get a pointer to physical memory...
   //
   // - Create the name
   // - Initialize the data to find the object
   // - Open a handle to the oject and check the status
   // - Get a pointer to the object
   // - Free the handle
   //

   RtlInitUnicodeString (&physicalMemoryUnicodeString,
                       L"\\Device\\PhysicalMemory");

   InitializeObjectAttributes (&objectAttributes,
                             &physicalMemoryUnicodeString,
                             OBJ_CASE_INSENSITIVE,
                             (HANDLE) NULL,
                             (PSECURITY_DESCRIPTOR) NULL);

   ntStatus = ZwOpenSection (&physicalMemoryHandle,
                           SECTION_ALL_ACCESS,
                           &objectAttributes);

   if (!NT_SUCCESS(ntStatus))
   {
      CoMemKdPrint (("COMEM.SYS: ZwOpenSection failed\n"));

      goto done;
   }

    ntStatus = ObReferenceObjectByHandle (physicalMemoryHandle,
                                          SECTION_ALL_ACCESS,
                                          (POBJECT_TYPE) NULL,
                                          KernelMode,
                                          &PhysicalMemorySection,
                                          (POBJECT_HANDLE_INFORMATION) NULL);

    if (!NT_SUCCESS(ntStatus))
    {
        CoMemKdPrint (("COMEM.SYS: ObReferenceObjectByHandle failed\n"));

        goto close_handle;
    }

    //
    // Initialize the physical addresses that will be translated
    //
    physicalAddressEnd = RtlLargeIntegerAdd (physicalAddress,
                                             RtlConvertUlongToLargeInteger(
                                             length));

    //
    // Translate the physical addresses.
    //
    translateBaseAddress =
        HalTranslateBusAddress (interfaceType,
                                busNumber,
                                physicalAddress,
                                &inIoSpace,
                                &physicalAddressBase);

    translateEndAddress =
        HalTranslateBusAddress (interfaceType,
                                busNumber,
                                physicalAddressEnd,
                                &inIoSpace2,
                                &physicalAddressEnd);

    if ( !(translateBaseAddress && translateEndAddress) )
    {
        CoMemKdPrint (("COMEM.SYS: HalTranslatephysicalAddress failed\n"));

        ntStatus = STATUS_UNSUCCESSFUL;

        goto close_handle;
    }

    //
    // Calculate the length of the memory to be mapped
    //
    mappedLength = RtlLargeIntegerSubtract (physicalAddressEnd,
                                            physicalAddressBase);

    //
    // If the mappedlength is zero, somthing very weird happened in the HAL
    // since the Length was checked against zero.
    //
    if (mappedLength.LowPart == 0)
    {
        CoMemKdPrint (("COMEM.SYS: mappedLength.LowPart == 0\n"));

        ntStatus = STATUS_UNSUCCESSFUL;

        goto close_handle;
    }

    length = mappedLength.LowPart;


    //
    // If the address is in io space, just return the address, otherwise
    // go through the mapping mechanism
    //
    if (inIoSpace)
    {
        *LinearBarAddress = (PVOID) physicalAddressBase.LowPart;
    }
    else
    {
        //
        // initialize view base that will receive the physical mapped
        // address after the MapViewOfSection call.
        //
        viewBase = physicalAddressBase;


        //
        // Let ZwMapViewOfSection pick an address
        //
        virtualAddress = NULL;



        //
        // Map the section
        //
        ntStatus = ZwMapViewOfSection (physicalMemoryHandle,
                                       (HANDLE) -1,
                                       &virtualAddress,
                                       0L,
                                       length,
                                       &viewBase,
                                       &length,
                                       ViewShare,
                                       0,
                                       PAGE_READWRITE | PAGE_NOCACHE);

        if (!NT_SUCCESS(ntStatus))
        {
            CoMemKdPrint (("COMEM.SYS: ZwMapViewOfSection failed\n"));

            goto close_handle;
        }

        //
        // Mapping the section above rounded the physical address down to the
        // nearest 64 K boundary. Now return a virtual address that sits where
        // we wnat by adding in the offset from the beginning of the section.
        //
        (ULONG) virtualAddress += (ULONG)physicalAddressBase.LowPart -
                                  (ULONG)viewBase.LowPart;

        *LinearBarAddress = virtualAddress;

    }
    ntStatus = STATUS_SUCCESS;

close_handle:

    ZwClose (physicalMemoryHandle);

done:

    return ntStatus;
}



//////////////////////////////////////////////////////////////////////////
// 
// Routine Description:
//
//     Just delete the associated device & return.
// 
// Arguments:
// 
//     DeviceObject - pointer to a device object
// 
// Return Value:
// 
// 
//////////////////////////////////////////////////////////////////////////
VOID
CoMemUnload(
    IN PDRIVER_OBJECT DriverObject
    )
{
   UNICODE_STRING         deviceLinkUnicodeString;
   PDEVICE_EXTENSION      deviceExtension;
   PDEVICE_OBJECT         deviceObject,temp;


   CoMemKdPrint (("COMEM.SYS: enter CoMemUnload\n"));

   deviceObject = DriverObject->DeviceObject;

   //
   // Traverse the list of device objects associated with the
   // driver object and delete them
   //
   while (deviceObject)
   {
      deviceExtension = (PDEVICE_EXTENSION) deviceObject->DeviceExtension;

      CoMemKdPrint (("COMEM.SYS: deleting device object %d\n",deviceExtension->instance));

      //
      // Delete the symbolic link
      //

      RtlInitUnicodeString (&deviceLinkUnicodeString,
                            deviceExtension->DeviceLinkNameBuffer);

      IoDeleteSymbolicLink (&deviceLinkUnicodeString);

      //
      // Delete the device object
      //
      temp = deviceObject;
      deviceObject = deviceObject->NextDevice;
      IoDeleteDevice (temp);
   }
   CoMemKdPrint (("COMEM.SYS: exit CoMemUnload\n"));
}


//////////////////////////////////////////////////////////////////////////
// 
// Routine Description:
//
//    returns the PCI config header for the first device on the bus with
//    vendor and device id's that match the input parameters.
//
// Arguments:
// 
//    VendorId - PCI vendor ID of the device
//    DeviceId - PCI device ID of the device
//    Instance - for systems with multiple devices, this parameter specifies
//               which one to return configuration data for.  Instances start
//               at zero.
//    PciCfgHdr - If a matching device and instance is found, this parameter
//               will contain the PCI Configuration Header
// 
// Return Value:
//    STATUS_SUCCESS - found he PCI device
//    STATUS_UNSUCCESSFUL - unable to find PCI device with matching vendor and
//                          device Id's
// 
// 
//////////////////////////////////////////////////////////////////////////
NTSTATUS
CoMemGetPciConfig(
      IN WORD VendorId,
      IN WORD DeviceId,
      IN int Instance,
      OUT PCI_CONFIG_HEADER_0 *PciCfgHdr
      )
{

   BUS_DATA_TYPE  BusDataType = PCIConfiguration;
   ULONG  BusNumber = 0;
   ULONG  SlotNumber;
   ULONG  Length;
   ULONG  myRet;

   CoMemKdPrint (("COMEM.SYS: Enter CoMemGetPciConfig\n"));

   Length = sizeof(PCI_CONFIG_HEADER_0);

   for (BusNumber = 0; BusNumber < 0x4; BusNumber++)
   {
      for (SlotNumber = 0; SlotNumber < 0xFF; SlotNumber++)
      {
         myRet = HalGetBusData(
                     BusDataType,
                     BusNumber,
                     SlotNumber,
                     PciCfgHdr,
                     Length);

         if (myRet > 2)
         {
            if ( PciCfgHdr->VendorID == VendorId && PciCfgHdr->DeviceID == DeviceId)
            {
               //
               // We found the device, if this is the correct instance return, otherwise
               // continue searching for another instance of this device
               //
               if (!Instance)
               {
                  CoMemKdPrint (("COMEM.SYS: Exit CoMemGetPciConfig\n"));
                  return STATUS_SUCCESS;
               }
               else
               {
                  Instance--;
               }
            }
            
         }
      }
   }
   CoMemKdPrint (("COMEM.SYS: Exit CoMemGetPciConfig\n"));
   return STATUS_UNSUCCESSFUL;

}


//////////////////////////////////////////////////////////////////////////
// 
// Routine Description:
//
//    Sets the the PCI config header for the first device on the bus with
//    vendor and device id's that match the input parameters.
//
// Arguments:
// 
//    VendorId - PCI vendor ID of the device
//    DeviceId - PCI device ID of the device
//    Instance - for systems with multiple devices, this parameter specifies
//               which one to set configuration data for.  Instances start
//               at zero.
//    PciCfgHdr - PCI COnfiguration Header to write to the device if found
// 
// Return Value:
//    STATUS_SUCCESS - found he PCI device
//    STATUS_UNSUCCESSFUL - unable to find PCI device with matching vendor and
//                          device Id's
// 
// 
//////////////////////////////////////////////////////////////////////////
NTSTATUS
CoMemSetPciConfig(
      IN WORD VendorId,
      IN WORD DeviceId,
      IN int Instance,
      IN PCI_CONFIG_HEADER_0 *PciCfgHdr
    )
{

   BUS_DATA_TYPE  BusDataType = PCIConfiguration;
   ULONG  BusNumber = 0;
   ULONG  SlotNumber;
   ULONG  Length;
   ULONG  myRet;
   PCI_CONFIG_HEADER_0 PciCfg;

   Length = sizeof(PCI_CONFIG_HEADER_0);

   for (BusNumber = 0; BusNumber < 0x4; BusNumber++)
   {
      for (SlotNumber = 0; SlotNumber < 0xFF; SlotNumber++)
      {
         myRet = HalGetBusData(
                     BusDataType,
                     BusNumber,
                     SlotNumber,
                     &PciCfg,
                     Length);

         if (myRet > 2)
         {
            if ( PciCfg.VendorID == VendorId && PciCfg.DeviceID == DeviceId)
            {
               //
               // We found the device, if this is the correct instance set the PCI config,
               // otherwise continue searching for another instance of this device
               //
               if (!Instance)
               {
                  HalSetBusData(
                     BusDataType,
                     BusNumber,
                     SlotNumber,
                     PciCfgHdr,
                     Length);
                  return STATUS_SUCCESS;
               }
               else
               {
                  Instance--;
               }
            }
            
         }
      }
   }
   return STATUS_UNSUCCESSFUL;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -