int device_probe (struct pci_dev* device, const struct pci_device_id* device_id) { struct AnBDevice* private_data = NULL; unsigned int flags = 0; int retval = 0; down(&sem); printk(KERN_INFO ANB_DEVICE_PREFIX "----------------------------------------probing---------------------------------------\n"); if(devices_count >= ANB_DEVICE_LIMIT) { printk(KERN_INFO ANB_DEVICE_PREFIX "The limit of devices has been reached\n"); return -EINVAL; } printk(KERN_INFO ANB_DEVICE_PREFIX "Device number: %d\n", devices_count); printk(KERN_INFO ANB_DEVICE_PREFIX "Allocating a memory...\n"); private_data = kzalloc(sizeof(*private_data), GFP_KERNEL); if (!private_data) { retval = -ENOMEM; goto probing_done; } printk(KERN_INFO ANB_DEVICE_PREFIX "Allocated kernel logical memory: 0x%016lx\n", (unsigned long)private_data); printk(KERN_INFO ANB_DEVICE_PREFIX " 0x%016lx\n", (unsigned long)private_data + sizeof(*private_data)); printk(KERN_INFO ANB_DEVICE_PREFIX " memory size: %ld bytes\n", sizeof(*private_data)); printk(KERN_INFO ANB_DEVICE_PREFIX "Enabling a device...\n"); retval = pci_enable_device(device); if (retval) goto probing_fb_free; printk(KERN_INFO ANB_DEVICE_PREFIX "Requesting memory regions...\n"); retval = pci_request_regions(device, ANB_DEVICE_NAME); if (retval) goto probing_fb_disable; printk(KERN_INFO ANB_DEVICE_PREFIX "Checking BARs configuration...\n"); print_bar_info(device); flags = pci_resource_flags(device, ANB_DEVICE_REGISTERS_BAR); private_data->bars[ANB_DEVICE_REGISTERS_BAR].offset = pci_resource_start(device, ANB_DEVICE_REGISTERS_BAR); private_data->bars[ANB_DEVICE_REGISTERS_BAR].length = pci_resource_len(device, ANB_DEVICE_REGISTERS_BAR); if((flags & IORESOURCE_IO) != IORESOURCE_IO) { printk(KERN_WARNING ANB_DEVICE_PREFIX "Unexpected BAR type (device registers)\n"); retval = -EINVAL; goto probing_fb_regions; } flags = pci_resource_flags(device, ANB_DEVICE_MEMORY_BAR); private_data->bars[ANB_DEVICE_MEMORY_BAR].offset = pci_resource_start(device, ANB_DEVICE_MEMORY_BAR); private_data->bars[ANB_DEVICE_MEMORY_BAR].length = pci_resource_len(device, ANB_DEVICE_MEMORY_BAR); if((flags & IORESOURCE_MEM) != IORESOURCE_MEM) { printk(KERN_WARNING ANB_DEVICE_PREFIX "Unexpected BAR type (device memory)\n"); retval = -EINVAL; goto probing_fb_regions; } printk(KERN_INFO ANB_DEVICE_PREFIX "Setting the device up...\n"); pci_write_config_dword(device, PCI_BASE_ADDRESS_0 + 4 * ANB_DEVICE_REGISTERS_BAR, private_data->bars[ANB_DEVICE_REGISTERS_BAR].offset); pci_write_config_dword(device, PCI_BASE_ADDRESS_0 + 4 * ANB_DEVICE_MEMORY_BAR, private_data->bars[ANB_DEVICE_MEMORY_BAR].offset); pci_write_config_word(device, PCI_COMMAND, ANB_DEVICE_COMMAND); pci_write_config_byte(device, PCI_LATENCY_TIMER, ANB_DEVICE_LATENCY); printk(KERN_INFO ANB_DEVICE_PREFIX "Mapping device registers into the memory...\n"); private_data->bars[ANB_DEVICE_REGISTERS_BAR].virt_address = pci_iomap(device, ANB_DEVICE_REGISTERS_BAR, private_data->bars[ANB_DEVICE_REGISTERS_BAR].length); if(!private_data->bars[ANB_DEVICE_REGISTERS_BAR].virt_address) { printk(KERN_WARNING ANB_DEVICE_PREFIX "Can't map BAR into the memory (device registers)\n"); retval = -ENOMEM; goto probing_fb_regions; } private_data->bars[ANB_DEVICE_MEMORY_BAR].virt_address = pci_iomap(device, ANB_DEVICE_MEMORY_BAR, private_data->bars[ANB_DEVICE_MEMORY_BAR].length); if(!private_data->bars[ANB_DEVICE_MEMORY_BAR].virt_address) { printk(KERN_WARNING ANB_DEVICE_PREFIX "Can't map BAR into the memory (device memory)\n"); retval = -ENOMEM; goto probing_fb_io_map; } printk(KERN_INFO ANB_DEVICE_PREFIX "Allocating entry in /dev...\n"); pci_set_drvdata(device, private_data); private_data->device_ref = device; private_data->device_id = devices_count; retval = AllocateEntry(private_data); if(retval) goto probing_fb_mem_map; devices_count += 1; goto probing_done; probing_fb_mem_map: pci_iounmap(device, private_data->bars[ANB_DEVICE_MEMORY_BAR].virt_address); private_data->bars[ANB_DEVICE_MEMORY_BAR].virt_address = NULL; probing_fb_io_map: pci_iounmap(device, private_data->bars[ANB_DEVICE_REGISTERS_BAR].virt_address); private_data->bars[ANB_DEVICE_REGISTERS_BAR].virt_address = NULL; probing_fb_regions: pci_release_regions(device); probing_fb_disable: pci_disable_device(device); probing_fb_free: kfree(private_data); private_data = NULL; probing_done: printk(KERN_INFO ANB_DEVICE_PREFIX "Probing retval: %d\n", retval); up(&sem); return retval; }
UPK_STATUS NitroPlus:: InitIndex( PVOID EntryBuffer, NITRO_PLUS_NPA_HEADER *Header ) { ULONG Length, Flags, HeaderSize, Hash; PBYTE Buffer; NITRO_PLUS_ENTRY *Entry; NITRO_PLUS_NPA_ETNRY *Info; m_Index.EntrySize = sizeof(*Entry); Entry = (NITRO_PLUS_ENTRY *)AllocateEntry(Header->EntryCount); if (Entry == NULL) return STATUS_INSUFFICIENT_RESOURCES; Buffer = (PBYTE)EntryBuffer; HeaderSize = sizeof(*Header) + Header->EntrySize; Flags = (Header->IsCompressed ? UNPACKER_ENTRY_COMPRESSED : 0) | (Header->IsEncrypted ? UNPACKER_ENTRY_ENCRYPTED : 0); Hash = Header->Hash[0] * Header->Hash[1]; for (ULONG Index = 0, Count = Header->EntryCount; Count; ++Index, --Count) { Length = *(PULONG)Buffer; Buffer += sizeof(Length); DecryptName(Buffer, Length, Index, Header); MultiByteToWideChar( IsShiftJISString((PSTR)Buffer, Length) ? CP_SHIFTJIS : CP_GB2312, 0, (PSTR)Buffer, Length, Entry->FileName, countof(Entry->FileName) ); Buffer += Length; Info = (NITRO_PLUS_NPA_ETNRY *)Buffer; switch (Info->FileType) { case NP_FILE_TYPE_DIRECTORY: Entry->Attributes = FILE_ATTRIBUTE_DIRECTORY; break; case NP_FILE_TYPE_FILE: default: Entry->Attributes = FILE_ATTRIBUTE_NORMAL; Entry->DirectoryIndex = Info->DirectoryIndex; Entry->Offset.QuadPart = Info->Offset + HeaderSize; Entry->CompressedSize.QuadPart = Info->CompressedSize; Entry->Size.QuadPart = Info->OriginalSize; Entry->Flags = Flags; Entry->Seed = (Hash + HashBuffer(Buffer - Length, Length)) * Entry->Size.LowPart; Entry->DecryptLength = MY_MIN(Entry->CompressedSize.LowPart, Length + 0x1000); break; } Buffer += sizeof(*Info); ++Entry; } m_Index.FileCount.QuadPart = Header->EntryCount; return STATUS_SUCCESS; }