script to make XP source files USB friently

Discuss & post Update Pack addons here.
Post Reply
travma
Posts: 7
Joined: Fri Feb 06, 2015 1:59 am

script to make XP source files USB friently

Post by travma » Thu Jun 08, 2017 9:46 am

usbfriend.7z
(3.53 KiB) Downloaded 36 times
The script is based on the FedoraForum.org article and is updated to support unicode sources. Just place it in a .vbs file and run it.

Code: Select all

'Warning, the usbfriend vbs script does not take backups.
'You should browse in a disk for the XP I386 source folder
'or you will cancel the browse to abort any modifications.
'The patch take just a second and you are informed
'with "Patching done !" message.
'This script work for official XP SP3 sources or for
'modified files by rvm integrator/nlite/DriverPacks
'from any languange. At the XP installation phase you
'need to have the disk connected but not afterwards.
'Note that a USB flash will stop or became readonly
'at any time because of wearing. XP setup and run is much
'faster in NTFS formated drive than FAT32. At USB boot
'windows will complain and disable the pagefile if you
'do not have hard disk.
Option Explicit

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const ReadOnly = 1
Const Hidden = 2
Const DelKey = 0
Const AssignValueToKey = 1
Const WriteKeyAsLine = 2
Dim unic
 
Function GetFileContents(Filename)
   Dim bomline, fb, sb, tb

   unic = False
   On Error Resume Next 
   bomline = CreateObject("Scripting.FileSystemObject").OpenTextFile(Filename, ForReading, False, False).Readline
   If Err.Number = 53 Then   'If Filename does not exist then create an empty ascii file
      On Error Goto 0
      CreateObject("Scripting.FileSystemObject").CreateTextFile(Filename, True, unic).Write ""
      Exit Function
   ElseIf Err.Number <> 0 Then
      MsgBox Err.Description & " for file: " & Filename, vbOKOnly+vbCritical, "GetFileContents aborting error:" & Err.Number
      Wscript.Quit
   End If
   On Error Goto 0

   If Len(bomline) > 2 then
      fb = Asc(bomline)
      sb = Asc(mid(bomline,2,1))
      tb = Asc(mid(bomline,3,1))
      If (fb = 255 and sb = 254) or (fb = 254 and sb = 255) or (fb = 239 and sb = 187 and tb = 191) Then
         unic = True
      End If
   ElseIf Len(bomline) = 2 then
      fb = Asc(bomline)
      sb = Asc(mid(bomline,2,1))
      If (fb = 255 and sb = 254) or (fb = 254 and sb = 255) Then
         unic = True
      End If
   End If
   GetFileContents = CreateObject("Scripting.FileSystemObject").OpenTextFile(Filename, ForReading, False, unic).ReadAll
End Function

Sub SetFileContents(Filename, Contents)
   On Error Resume Next

   CreateObject("Scripting.FileSystemObject").CreateTextFile(Filename, True, unic).Write Contents
   If Err.Number <> 0 Then
      MsgBox Err.Description & " for file: " & Filename, vbOKOnly+vbCritical, "SetFileContents aborting error:" & Err.Number
      Wscript.Quit
  End If
End Sub

'The Section searched for Key must pass as [sectionlabel]
'If Action = AssignValueToKey, the Key searched inside the inf/sif/ini file
'must be followed by the = so, Value will replace anything after that =
'If the Key does not exist in the file, it will created
'along with the = Value just below the Section
'If the Key inside the file is comment out, it will uncomment
'The Key or Section will also created if it is not there
'
'If Action = WriteKeyAsLine the Key should contain the whole declaration
'line (with or without the =)
'That line will inserted below the Section (if it is not already there)
'
'If Action = DelKey the Key will searched and the coresponing line will
'be deleted form the file
'In case of WriteKeyAsLine and DelKey the Value parameter is ignored.
'
'If File does not exist the WriteInfValue it will create an ascii file
'If the File exist it will keep the same encoding (unicode or ascii)
'
'Note, iniman.exe from 2003 RK, Read & Write Ini from robvanderwoude.com
'and westphil.nl aren't sufficient for the job
Sub WriteInfValue(File, Section, Key, Value, Action)
   Dim SectSP, SectEP, KeySP, ValSP, LineSP, LineEP, SectCont, inftxt, inftxtnew

   If File = "" Or Section = "" Or Key = "" Then Exit Sub

   inftxt = GetFileContents(File)
   SectSP = InStr(1, inftxt, Section, vbBinaryCompare)
   If SectSP > 0 Then
      SectEP = InStr(SectSP + Len(Section), inftxt, "[", vbBinaryCompare)
      If SectEP = 0 Then
         SectEP = Len(inftxt) + 1
      End If
      SectCont = Mid(inftxt, SectSP + Len(Section), SectEP - SectSP - Len(Section))
   
      KeySP = Instr(1, SectCont, Key, vbBinaryCompare)
      If KeySP > 0 Then 
         LineSP = InStrRev(SectCont, vbCrLf, KeySP - 1, vbBinaryCompare)
         If LineSP = 0 Then
            LineSP = 1
         End If
         If Action = AssignValueToKey Then
            ValSP = Instr(KeySP + Len(Key), SectCont, "=", vbBinaryCompare)
            If ValSP > 0 Then
               Do While Mid(SectCont, ValSP + 1, 1) = " " or Mid(SectCont, ValSP + 1, 1) = vbTab
                  ValSP = ValSP + 1
               Loop

               LineEP = Instr(ValSP + 1, SectCont, vbCrLf, vbBinaryCompare)
               If LineEP = 0 Then
                  LineEP = Len(SectCont) + 1
               End If
               
               If InStr(1, Mid(SectCont, LineSP, KeySP - LineSP), ";", vbBinaryCompare) = 0 Then
                  inftxtnew = Left(SectCont, ValSP) & Value & Mid(SectCont, LineEP) 
                  SetFileContents File, Left(inftxt, SectSP + Len(Section) - 1) & inftxtnew & Mid(inftxt, SectEP)
               Else    ' comment found in front of Key, discard everything before KeySP 
                  inftxtnew = Left(SectCont, LineSP - 1) & vbCrLf & Mid(SectCont, KeySP, ValSP - KeySP + 1) & Value & Mid(SectCont, LineEP)
                  SetFileContents File, Left(inftxt, SectSP + Len(Section) - 1) & inftxtnew & Mid(inftxt, SectEP)
               End If
            End If         'Else Key found but without =, probably a comment, corrupt file or user pass wrong parameters
         ElseIf Action = DelKey Then
		    LineEP = Instr(KeySP + Len(Key), SectCont, vbCrLf, vbBinaryCompare)
            If LineEP = 0 Then
               LineEP = Len(SectCont) + 1
            End If
            If LineSP > 0 Then
               SetFileContents File, Left(inftxt, SectSP + Len(Section) - 1) & Left(SectCont, LineSP - 1) & mid(SectCont, LineEP) & Mid(inftxt, SectEP)
            End If    
         End If
      ElseIf Action = AssignValueToKey Then
         inftxt = Left(inftxt, SectSP + Len(Section) - 1) & vbCrLf & Key & " = " & Value & Mid(inftxt, SectSP + Len(Section))
         SetFileContents File, inftxt
      ElseIF Action = WriteKeyAsLine Then
         SetFileContents File, Left(inftxt, SectSP + Len(Section) - 1) & vbCrLf & Key & Mid(inftxt, SectSP + Len(Section))
      End If
   ElseIf Action = WriteKeyAsLine Then
      If Len(inftxt) > 0 Then
         SetFileContents File, inftxt & vbCrLf & Section & vbCrLf & Key & vbCrLf
      Else
         SetFileContents File, Section & vbCrLf & Key
      End If
   ElseIf Action = AssignValueToKey Then
      SetFileContents File, inftxt & vbCrLf & Section & vbCrLf & Key & " = " & Value & vbCrLf
   End If
End Sub


dim odir, oShell, ofs, i386

Set odir = CreateObject("Shell.Application" ).BrowseForFolder( 0, "Select XP I386 folder." & vbCrLf & "Files needed: USB.IN_ USBPORT.IN_ USBSTOR.IN_ TXTSETUP.SIF DOSNET.INF", 0, "")
If odir Is Nothing Then wscript.quit            'user cancel the browse selection to the i386 folder
If not odir.self.IsFileSystem Then wscript.quit 'user select a virtual object folder like My Computer
If Right(odir.Self.Path, 1) <> "\" Then
   i386 = odir.Self.Path & "\"
Else
   i386 = odir.Self.Path
End If
Set ofs = CreateObject("Scripting.FileSystemObject")
if not (ofs.FileExists(i386 & "USB.IN_") and ofs.FileExists(i386 & "USBPORT.IN_") and ofs.FileExists(i386 & "USBSTOR.IN_") and ofs.FileExists(i386 & "TXTSETUP.SIF") and ofs.FileExists(i386 & "DOSNET.INF")) Then
   MsgBox "The " & i386 & " folder does not have the needed files.", vbOKOnly+vbExclamation, "Nothing is changed, aborting"
   Wscript.Quit
End If

If ofs.GetFile(i386 & "TXTSETUP.SIF").Attributes and ReadOnly then
   ofs.GetFile(i386 & "TXTSETUP.SIF").Attributes = ofs.GetFile(i386 & "TXTSETUP.SIF").Attributes XOR ReadOnly
End If
If ofs.GetFile(i386 & "DOSNET.INF").Attributes and ReadOnly then
   ofs.GetFile(i386 & "DOSNET.INF").Attributes = ofs.GetFile(i386 & "DOSNET.INF").Attributes XOR ReadOnly
End If

Set oShell = CreateObject ("WScript.Shell")
oShell.run "expand -r " & i386 & "USB.IN_ " & i386 & "USBPORT.IN_ " & I386 & "USBSTOR.IN_", 0, True

WriteInfValue i386 & "usb.inf", "[StandardHub.AddService]", "StartType", "0", AssignValueToKey
WriteInfValue i386 & "usb.inf", "[StandardHub.AddService]", "LoadOrderGroup", "Boot Bus Extender", AssignValueToKey
WriteInfValue i386 & "usb.inf", "[CommonClassParent.AddService]", "StartType", "0", AssignValueToKey
WriteInfValue i386 & "usb.inf", "[CommonClassParent.AddService]", "LoadOrderGroup", "Boot Bus Extender", AssignValueToKey

WriteInfValue i386 & "usbport.inf", "[EHCI.AddService]", "StartType", "0", AssignValueToKey
WriteInfValue i386 & "usbport.inf", "[EHCI.AddService]", "LoadOrderGroup", "Boot Bus Extender", AssignValueToKey
WriteInfValue i386 & "usbport.inf", "[OHCI.AddService]", "StartType", "0", AssignValueToKey
WriteInfValue i386 & "usbport.inf", "[OHCI.AddService]", "LoadOrderGroup", "Boot Bus Extender", AssignValueToKey
WriteInfValue i386 & "usbport.inf", "[UHCI.AddService]", "StartType", "0", AssignValueToKey
WriteInfValue i386 & "usbport.inf", "[UHCI.AddService]", "LoadOrderGroup", "Boot Bus Extender", AssignValueToKey
WriteInfValue i386 & "usbport.inf", "[ROOTHUB.AddService]", "StartType", "0", AssignValueToKey
WriteInfValue i386 & "usbport.inf", "[ROOTHUB.AddService]", "LoadOrderGroup", "Boot Bus Extender", AssignValueToKey

WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "DisplayName", , DelKey
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "ServiceType", , DelKey
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "StartType", , DelKey
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "ErrorControl", , DelKey
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "ServiceBinary", , DelKey
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "Tag", , DelKey
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "LoadOrderGroup", , DelKey

WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "LoadOrderGroup = Boot Bus Extender", , WriteKeyAsLine
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "ServiceBinary  = %12%\USBSTOR.SYS", , WriteKeyAsLine
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "ErrorControl   = 1", , WriteKeyAsLine
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "Tag = 3", , WriteKeyAsLine
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "StartType      = 0", , WriteKeyAsLine
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "ServiceType    = 1", , WriteKeyAsLine
WriteInfValue i386 & "usbstor.inf", "[USBSTOR.AddService]", "DisplayName    = %USBSTOR.SvcDesc%", , WriteKeyAsLine

WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport.Load]", "usbehci", , DelKey
WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport.Load]", "usbohci", , DelKey
WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport.Load]", "usbuhci", , DelKey
WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport.Load]", "usbhub", , DelKey
WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport.Load]", "usbstor", , DelKey

WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders.Load]", "usbehci", "usbehci.sys", AssignValueToKey
WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders.Load]", "usbohci", "usbohci.sys", AssignValueToKey
WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders.Load]", "usbuhci", "usbuhci.sys", AssignValueToKey
WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders.Load]", "usbhub", "usbhub.sys", AssignValueToKey
WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders.Load]", "usbstor", "usbstor.sys", AssignValueToKey

WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport]", "usbehci", , DelKey
WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport]", "usbohci", , DelKey
WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport]", "usbuhci", , DelKey
WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport]", "usbhub", , DelKey
WriteInfValue i386 & "TXTSETUP.SIF", "[InputDevicesSupport]", "usbstor", , DelKey

WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders]", "usbehci", """Enhanced Host Controller"",files.usbehci,usbehci", AssignValueToKey
WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders]", "usbohci", """Open Host Controller"",files.usbohci,usbohci", AssignValueToKey
WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders]", "usbuhci", """Universal Host Controller"",files.usbuhci,usbuhci", AssignValueToKey
WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders]", "usbhub", """Generic USB Hub Driver"",files.usbhub,usbhub", AssignValueToKey
WriteInfValue i386 & "TXTSETUP.SIF", "[BootBusExtenders]", "usbstor", """USB Storage Class Driver"",files.usbstor,usbstor", AssignValueToKey

WriteInfValue i386 & "TXTSETUP.SIF", "[HiveInfs.Fresh]", "AddReg = usbboot.inf,usbservices", , WriteKeyAsLine
WriteInfValue i386 & "TXTSETUP.SIF", "[SourceDisksFiles]", "usbboot.inf", "1,,,,,,_x,3,,3", AssignValueToKey

WriteInfValue i386 & "DOSNET.INF", "[Files]", "d1,usbboot.inf", , WriteKeyAsLine

oShell.run "cabarc -m LZX:21 n " & i386 & "USB.IN_ " & i386 & "USB.INF", 0, True
oShell.run "cabarc -m LZX:21 n " & i386 & "USBPORT.IN_ " & I386 & "USBPORT.INF", 0, True
oShell.run "cabarc -m LZX:21 n " & i386 & "USBSTOR.IN_ " & I386 & "USBSTOR.INF", 0, True

ofs.DeleteFile i386 & "USB.INF"
ofs.DeleteFile i386 & "USBPORT.INF"
ofs.DeleteFile i386 & "USBSTOR.INF"

WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbohci"",""Type"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbohci"",""Start"",0x00010001,0", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbohci"",""ImagePath"",0x00020000,""system32\DRIVERS\usbohci.sys""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbohci"",""Group"",0x00000000,""System Reserved""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbohci"",""ErrorControl"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", vbCrLf & "HKLM,""SYSTEM\CurrentControlSet\Services\usbohci"",""DisplayName"",0x00000000,""Microsoft USB Open Host Controller Miniport Driver""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbuhci"",""Type"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbuhci"",""Start"",0x00010001,0", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbuhci"",""ImagePath"",0x00020000,""system32\DRIVERS\usbuhci.sys""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbuhci"",""Group"",0x00000000,""System Reserved""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbuhci"",""ErrorControl"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", vbCrLf & "HKLM,""SYSTEM\CurrentControlSet\Services\usbuhci"",""DisplayName"",0x00000000,""Microsoft USB Universal Host Controller Miniport Driver""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbhub"",""Type"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbhub"",""Start"",0x00010001,0", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbhub"",""ImagePath"",0x00020000,""system32\DRIVERS\usbhub.sys""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbhub"",""Group"",0x00000000,""System Reserved""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbhub"",""ErrorControl"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", vbCrLf & "HKLM,""SYSTEM\CurrentControlSet\Services\usbhub"",""DisplayName"",0x00000000,""USB2 Enabled Hub""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbehci"",""Type"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbehci"",""Start"",0x00010001,0", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbehci"",""ImagePath"",0x00020000,""system32\DRIVERS\usbehci.sys""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbehci"",""Group"",0x00000000,""System Reserved""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\usbehci"",""ErrorControl"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", vbCrLf & "HKLM,""SYSTEM\CurrentControlSet\Services\usbehci"",""DisplayName"",0x00000000,""USB 2.0 Enhanced Host Controller Miniport Driver""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\USBSTOR"",""Type"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\USBSTOR"",""Start"",0x00010001,0", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\USBSTOR"",""ImagePath"",0x00020000,""system32\DRIVERS\USBSTOR.SYS""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\USBSTOR"",""Group"",0x00000000,""System Reserved""", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", "HKLM,""SYSTEM\CurrentControlSet\Services\USBSTOR"",""ErrorControl"",0x00010001,1", , WriteKeyAsLine
WriteInfValue i386 & "USBBOOT.INF", "[usbservices]", vbCrLf & "HKLM,""SYSTEM\CurrentControlSet\Services\USBSTOR"",""DisplayName"",0x00000000,""USB Mass Storage Driver""", , WriteKeyAsLine
MsgBox "Patching done !", vbOKOnly+vbInformation, "usbfriend script"

Post Reply