@@ -51,7 +51,7 @@ static void Main(string[] args)
5151 {
5252 // This is a command (like 'devices', 'flash', 'getvar')
5353 string command = arg ;
54-
54+
5555 // Collect arguments for this specific command until next arg starting with '-'
5656 List < string > commandArgs = new List < string > ( ) ;
5757 while ( i < args . Length && ! args [ i ] . StartsWith ( "-" ) )
@@ -68,11 +68,35 @@ static void Main(string[] args)
6868 return ;
6969 }
7070
71+ if ( pendingCommands . Count == 1 && pendingCommands [ 0 ] . Command == "devices" )
72+ {
73+ ExecuteDeviceList ( pendingCommands [ 0 ] . Args ) ;
74+ return ;
75+ }
76+
77+ var devices = UsbManager . GetAllDevices ( ) ;
78+ UsbDevice ? target = serial != null ? devices . FirstOrDefault ( d => d . SerialNumber == serial ) : ( devices . Count > 0 ? devices [ 0 ] : null ) ;
79+
80+ if ( target == null )
81+ {
82+ Console . Error . WriteLine ( "fastboot: error: no devices/found" ) ;
83+ Environment . Exit ( 1 ) ;
84+ }
85+
86+ using FastbootUtil util = new FastbootUtil ( target ) ;
87+ if ( sparseLimit . HasValue ) FastbootUtil . SparseMaxDownloadSize = ( int ) Math . Min ( int . MaxValue , sparseLimit . Value ) ;
88+
89+ util . ReceivedFromDevice += ( s , e ) =>
90+ {
91+ if ( e . NewInfo != null ) Console . Error . WriteLine ( "(bootloader) " + e . NewInfo ) ;
92+ } ;
93+
7194 foreach ( var cmd in pendingCommands )
7295 {
7396 try
7497 {
75- ExecuteCommand ( cmd . Command , cmd . Args ) ;
98+ util . ResetTransport ( ) ;
99+ ExecuteCommand ( util , cmd . Command , cmd . Args ) ;
76100 }
77101 catch ( Exception ex )
78102 {
@@ -83,6 +107,16 @@ static void Main(string[] args)
83107 }
84108 }
85109
110+ static void ExecuteDeviceList ( List < string > args )
111+ {
112+ bool verbose = args . Contains ( "-l" ) ;
113+ foreach ( var dev in UsbManager . GetAllDevices ( ) )
114+ {
115+ if ( verbose ) Console . WriteLine ( $ "{ dev . SerialNumber } \t fastboot { dev . GetType ( ) . Name } ") ;
116+ else Console . WriteLine ( $ "{ dev . SerialNumber } \t fastboot") ;
117+ }
118+ }
119+
86120 static long ParseSize ( string sizeStr )
87121 {
88122 long multiplier = 1 ;
@@ -93,31 +127,14 @@ static long ParseSize(string sizeStr)
93127 return long . Parse ( sizeStr ) * multiplier ;
94128 }
95129
96- static void ExecuteCommand ( string command , List < string > args )
130+ static void ExecuteCommand ( FastbootUtil util , string command , List < string > args )
97131 {
98132 if ( command == "devices" )
99133 {
100- bool verbose = args . Contains ( "-l" ) ;
101- foreach ( var dev in UsbManager . GetAllDevices ( ) )
102- {
103- if ( verbose ) Console . WriteLine ( $ "{ dev . SerialNumber } \t fastboot { dev . GetType ( ) . Name } ") ;
104- else Console . WriteLine ( $ "{ dev . SerialNumber } \t fastboot") ;
105- }
134+ ExecuteDeviceList ( args ) ;
106135 return ;
107136 }
108137
109- var devices = UsbManager . GetAllDevices ( ) ;
110- UsbDevice ? target = serial != null ? devices . FirstOrDefault ( d => d . SerialNumber == serial ) : ( devices . Count > 0 ? devices [ 0 ] : null ) ;
111-
112- if ( target == null ) throw new Exception ( "no devices/found" ) ;
113-
114- using FastbootUtil util = new FastbootUtil ( target ) ;
115- if ( sparseLimit . HasValue ) FastbootUtil . SparseMaxDownloadSize = ( int ) Math . Min ( int . MaxValue , sparseLimit . Value ) ;
116-
117- util . ReceivedFromDevice += ( s , e ) =>
118- {
119- if ( e . NewInfo != null ) Console . Error . WriteLine ( "(bootloader) " + e . NewInfo ) ;
120- } ;
121138 util . DataTransferProgressChanged += ( s , e ) =>
122139 {
123140 int percent = ( int ) ( e . Item1 * 100 / e . Item2 ) ;
@@ -134,30 +151,26 @@ static void ExecuteCommand(string command, List<string> args)
134151 util . FormatPartition ( "cache" ) ;
135152 }
136153
137- // Process partition name with slot suffix (Aligned with AOSP dynamic detection)
138154 string GetPartition ( string baseName )
139155 {
140156 if ( string . IsNullOrEmpty ( slot ) || slot == "all" )
141157 {
142- // If A/B device and no slot specified, auto-detect current slot
143158 if ( util . HasSlot ( baseName ) )
144159 {
145160 string current = util . GetCurrentSlot ( ) ;
146161 if ( ! string . IsNullOrEmpty ( current ) ) return baseName + "_" + current ;
147162 }
148163 return baseName ;
149164 }
150-
165+
151166 if ( slot == "other" )
152167 {
153168 string current = util . GetCurrentSlot ( ) ;
154169 string other = ( current == "a" ) ? "b" : "a" ;
155170 return baseName + "_" + other ;
156171 }
157172
158- // Explicit slot provided (a/b)
159173 if ( util . HasSlot ( baseName ) ) return baseName + "_" + slot ;
160-
161174 return baseName ;
162175 }
163176
@@ -167,7 +180,6 @@ string GetPartition(string baseName)
167180 string ? targetSlot = args . Count > 0 ? args [ 0 ] : slot ;
168181 if ( string . IsNullOrEmpty ( targetSlot ) )
169182 {
170- // AOSP: if no slot, toggle the current slot
171183 string ? current = util . GetVar ( "current-slot" ) ;
172184 targetSlot = ( current == "a" ) ? "b" : "a" ;
173185 }
@@ -214,7 +226,6 @@ string GetPartition(string baseName)
214226 case "flash" :
215227 bool disableVerity = args . Contains ( "--disable-verity" ) ;
216228 bool disableVerification = args . Contains ( "--disable-verification" ) ;
217- // Filter out flags from args
218229 var flashArgs = args . Where ( a => ! a . StartsWith ( "--" ) ) . ToList ( ) ;
219230 if ( flashArgs . Count < 1 ) throw new Exception ( "usage: fastboot flash <partition> [filename]" ) ;
220231 string part = GetPartition ( flashArgs [ 0 ] ) ;
@@ -240,9 +251,9 @@ string GetPartition(string baseName)
240251 {
241252 if ( force )
242253 {
243- // In a real AOSP fastboot, --force might bypass certain checks
244- // Here we just acknowledge it to silence the warning
254+ util . OemCommand ( "snapshot-update cancel" ) . ThrowIfError ( ) ;
245255 }
256+
246257 using var fs = File . OpenRead ( file ) ;
247258 util . FlashUnsparseImage ( part , fs , fs . Length ) . ThrowIfError ( ) ;
248259 }
@@ -282,10 +293,12 @@ string GetPartition(string baseName)
282293 break ;
283294
284295 case "set_active" :
285- // Handled before switch if invoked as 'fastboot set_active'
286- // This block is for if someone passes 'set_active' but it wasn't caught
287- string saSlot = args . Count > 0 ? args [ 0 ] : "" ;
288- if ( string . IsNullOrEmpty ( saSlot ) ) throw new Exception ( "usage: fastboot set_active <slot>" ) ;
296+ string ? saSlot = args . Count > 0 ? args [ 0 ] : slot ;
297+ if ( string . IsNullOrEmpty ( saSlot ) )
298+ {
299+ string ? current = util . GetVar ( "current-slot" ) ;
300+ saSlot = ( current == "a" ) ? "b" : "a" ;
301+ }
289302 util . SetActiveSlot ( saSlot ) . ThrowIfError ( ) ;
290303 break ;
291304
0 commit comments