Partitions.Mod 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. MODULE Partitions; (** AUTHOR "staubesv"; PURPOSE "Commandline front-end for PartitionsLib"; *)
  2. (**
  3. * This is the commandline front-end for PartitionsLib.
  4. *
  5. * Command overview:
  6. *
  7. * Uncritical operations:
  8. *
  9. * Partitions.Show ~ Show all disk devices and their partition layout
  10. * Partitions.Show detail ~ Show detailed information about all disk devices and their partition layout
  11. * Partitions.ShowAosFSLimits ~ Show limitations of the Aos File System
  12. *
  13. * Partitions.ShowOps~ Show all pending disk operations
  14. * Partitions.ShowOps detail ~ Show detailed information about all pending disk operations
  15. *
  16. * Partitions.ShowOp uid ~ Show detailed status of the specified operation
  17. * Partitions.Abort <opID> ~ Abort the disk operation with the specified operation ID
  18. * Partitions.Remove <opID> ~ Abort the disk operation with the specified operation ID and remove it from the operation manager
  19. *
  20. * Partitions.Check dev#part ~ Perform a read test on the specified partition
  21. * Partitions.Eject dev ~ Eject medium of the specified device
  22. * Partitions.Sync dev ~ Synchronize device caches to medium
  23. *
  24. * Partitions.Safe~ Disallow extremely critical operations
  25. * Partitions.Unsafe~ Allow extermely critical operations
  26. *
  27. * Partitions.ShowBlocks dev#part first nbr Show <nbr> blocks starting at block <first> of the specified partition
  28. *
  29. * Critical operations:
  30. *
  31. * Partitions.Create dev#part type sizeMB ~ Create a partition
  32. * Partitions.Delete dev#part type ~ Delete the specified partition
  33. * Partitions.Activate dev#part ~ Set the active bit (boot) of the specified partition
  34. * Partitions.Deactivate dev#part ~ Clear the active bit (boot) of the specified partition
  35. * Partitions.ChangeType dev#part from to ~ Change the type of the specified partition from <from> to <to>
  36. * Partitions.Format dev#part fs ~ Format the specified partition with the specified file system (AosFS, FatFS)
  37. * Partitions.WriteMBR dev#part ~ Write MBR boot code to specified partition (partition table will be untouched)
  38. *
  39. * Partitions.InstallBootManager dev#0 ~ Install boot manager
  40. * Partitions.PartitionToFile dev#part file f nbr~ Write <nbr> blocks starting at block <f> to the specified file.
  41. * Partitions.FileToPartition dev#part file f nbr~ Write the content of <file> to the specified partition starting at block <f>, <nbr> blocks
  42. *
  43. * Bluebottle-specific opertaions
  44. *
  45. * Partitions.UpdateBootFile dev#part bootfile Update the boot file (e.g. IDE.Bin) for the specified partition
  46. * Partitions.UpdateBootLoader dev#part bl Update the boot loader (e.g. OBL.Bin) for the specified partition
  47. *
  48. * Partitions.GetConfig dev#part ~ Get the config string of the specified partition
  49. * Partitions.SetConfig dev#part config Set the config string for the specified partition
  50. *
  51. *
  52. * History:
  53. *
  54. * 05.08.2005 Cleanup (staubesv)
  55. * 25.11.2005 Added ShowOp procedure (staubesv)
  56. * 06.01.2006 Small cleanup (staubesv)
  57. * 17.01.2006 WriteMBR: Caller can specify "DESTROY" parameter, fixed SetConfig (staubesv)
  58. *)
  59. IMPORT KernelLog, Texts, TextUtilities, Disks, Files, Lib := PartitionsLib, Plugins, Commands, Streams, Strings, FATScavenger;
  60. CONST
  61. Trace = FALSE;
  62. Invalid = MIN(LONGINT);
  63. (* InstallBootManager default arguments *)
  64. BootManagerMBRFile = "BootManagerMBR.Bin";
  65. BootManagerTailFile = "BootManagerTail.Bin";
  66. (** Show all currently pending disk operations *)
  67. PROCEDURE ShowOps*(context : Commands.Context); (** [detail] ~ *)
  68. VAR par : ARRAY 10 OF CHAR; details : BOOLEAN;
  69. BEGIN
  70. par := ""; context.arg.SkipWhitespace; context.arg.String(par);
  71. details := (par = "detail");
  72. Lib.operations.Show(context.out, details);
  73. END ShowOps;
  74. (** Show the detailed state of the specified operation *)
  75. PROCEDURE ShowOp*(context : Commands.Context); (** uid ~ *)
  76. VAR operation : Lib.Operation; uid : LONGINT;
  77. BEGIN
  78. IF context.arg.GetInteger(uid, FALSE) THEN
  79. operation := Lib.operations.GetByUid(uid);
  80. IF operation # NIL THEN
  81. operation.Show(context.out, TRUE);
  82. ELSE
  83. context.error.String("Error: Operation UID "); context.error.Int(uid, 0); context.error.String(" not found"); context.error.Ln;
  84. context.result := Commands.CommandError;
  85. END;
  86. ELSE
  87. context.error.String("Exspected parameters: uid"); context.error.Ln;
  88. context.result := Commands.CommandParseError;
  89. END;
  90. END ShowOp;
  91. (** Abort a running operation *)
  92. PROCEDURE Abort*(context : Commands.Context); (** uid ~ *)
  93. VAR operation : Lib.Operation; uid : LONGINT;
  94. BEGIN
  95. IF context.arg.GetInteger(uid, FALSE) THEN
  96. operation := Lib.operations.GetByUid(uid);
  97. IF operation # NIL THEN
  98. operation.Abort;
  99. context.out.String("Operation UID "); context.out.Int(uid, 0); context.out.String(" aborted"); context.out.Ln;
  100. ELSE
  101. context.error.String("Error: Operation UID "); context.error.Int(uid, 0); context.error.String(" not found"); context.error.Ln;
  102. context.result := Commands.CommandParseError;
  103. END;
  104. ELSE
  105. context.error.String("Exspected parameters: uid"); context.error.Ln;
  106. context.result := Commands.CommandError;
  107. END;
  108. END Abort;
  109. (** Remove (and if necessary abort) operations from the operations registry *)
  110. PROCEDURE Remove*(context : Commands.Context); (** uid | "all" | "finished" ~*)
  111. VAR par : ARRAY 128 OF CHAR; uid, num : LONGINT;
  112. BEGIN
  113. IF context.arg.GetInteger(uid, FALSE) THEN
  114. IF Lib.operations.RemoveByUid(uid) THEN
  115. context.out.String("Operation UID "); context.out.Int(uid, 0); context.out.String(" has been removed"); context.out.Ln;
  116. ELSE
  117. context.error.String("Error: Could not remove operation UID "); context.error.Int(uid, 0); context.error.Ln;
  118. context.result := Commands.CommandError;
  119. END;
  120. ELSIF context.arg.res = Streams.FormatError THEN
  121. par := "";
  122. context.arg.SetPos(0);
  123. IF context.arg.GetString(par) THEN
  124. Strings.UpperCase(par);
  125. IF par = "ALL" THEN
  126. num := Lib.operations.RemoveAll(TRUE);
  127. context.out.Int(num, 0); context.out.String(" operations have been removed"); context.out.Ln;
  128. ELSIF par = "FINISHED" THEN
  129. context.out.String("All finished operation have been removed"); context.out.Ln;
  130. ELSE
  131. context.error.String("Expected parameters: uid | all | finished"); context.error.Ln;
  132. context.result := Commands.CommandParseError;
  133. END;
  134. ELSE
  135. context.error.String("Expected parameters: uid | all | finished"); context.error.Ln;
  136. context.result := Commands.CommandParseError;
  137. END;
  138. ELSE
  139. context.error.String("Expected parameters: uid | all | finished"); context.error.Ln;
  140. context.result := Commands.CommandParseError;
  141. END;
  142. END Remove;
  143. PROCEDURE Mount*(context : Commands.Context);
  144. VAR
  145. mount :Lib.Mount;
  146. prefix, alias, volumePars, fsPars : ARRAY 64 OF CHAR;
  147. selection : Lib.Selection;
  148. BEGIN
  149. IF GetSelection(context, TRUE, selection) THEN
  150. volumePars := ""; fsPars := "";
  151. IF context.arg.GetString(alias) & context.arg.GetString(prefix) THEN
  152. NEW(mount, selection.disk, selection.partition, context.out);
  153. mount.SetParameters(prefix, alias, volumePars, fsPars);
  154. mount.SetBlockingStart;
  155. ELSE
  156. context.error.String("Expected parameters: dev#part alias prefix"); context.error.Ln;
  157. context.result := Commands.CommandError;
  158. END;
  159. END;
  160. END Mount;
  161. (* Format a partition with an N2KFS, AosFS or FatFS. *)
  162. PROCEDURE Format*(context : Commands.Context); (** dev#part [ "AosFS" | "NatFS" | "NatFS2" [ FSRes [ BootFile [ Flag ] ] ] ] | ["FatFS" ["Quick"]] ~ *)
  163. VAR
  164. formatAos : Lib.FormatPartition; formatFat : FATScavenger.FormatPartition;
  165. fsname, bootfile, quick : ARRAY 128 OF CHAR;
  166. fsRes, flags : LONGINT;
  167. quickFormat : BOOLEAN;
  168. selection : Lib.Selection;
  169. BEGIN
  170. IF GetSelection(context, TRUE, selection) THEN
  171. IF context.arg.GetString(fsname) THEN
  172. IF fsname = "FatFS" THEN
  173. quickFormat := context.arg.GetString(quick) & (quick = "Quick");
  174. NEW(formatFat, selection.disk, selection.partition, context.out);
  175. formatFat.SetParameters(Strings.NewString("no name"), quickFormat);
  176. formatFat.SetBlockingStart;
  177. context.out.String("Partitions UID "); context.out.Int(formatFat.uid, 0);
  178. context.out.String(": Started FAT format on "); context.out.String(formatFat.diskpartString); context.out.Ln;
  179. ELSIF (fsname = "AosFS") OR (fsname = "NatFS") OR (fsname = "NatFS1") OR (fsname = "NatFS2") THEN
  180. bootfile := ""; fsRes := -2; flags := 0;
  181. IF context.arg.GetInteger(fsRes, FALSE) THEN
  182. IF context.arg.GetString(bootfile) THEN
  183. context.arg.SkipWhitespace; context.arg.Int(flags, FALSE);
  184. END;
  185. END;
  186. NEW(formatAos, selection.disk, selection.partition, context.out);
  187. formatAos.SetParameters(fsname, bootfile, fsRes, flags);
  188. formatAos.SetBlockingStart;
  189. ELSE
  190. context.error.String("File system "); context.error.String(fsname); context.error.String(" is not supported"); context.error.Ln;
  191. context.result := Commands.CommandError;
  192. END;
  193. ELSE (* optional parameters not specified *)
  194. NEW(formatAos, selection.disk, selection.partition, context.out);
  195. formatAos.SetParameters("AosFS", "", -2, 0);
  196. formatAos.SetBlockingStart;
  197. context.out.String("Partitions UID "); context.out.Int(formatAos.uid, 0);
  198. context.out.String(": Started format on "); context.out.String(formatAos.diskpartString); context.out.Ln;
  199. END;
  200. END;
  201. END Format;
  202. (* Update the boot file in an existing Oberon partition. *)
  203. PROCEDURE UpdateBootFile*(context : Commands.Context); (** dev#part BootFile ~ *)
  204. VAR
  205. updateBootFile : Lib.UpdateBootFile;
  206. selection : Lib.Selection;
  207. filename : Files.FileName;
  208. BEGIN
  209. IF GetSelection(context, FALSE, selection) THEN
  210. IF context.arg.GetString(filename) THEN
  211. NEW(updateBootFile, selection.disk, selection.partition, context.out);
  212. updateBootFile.SetParameters(filename);
  213. updateBootFile.SetBlockingStart;
  214. ELSE
  215. context.error.String("Expected parameters: dev#part bootfilename"); context.error.Ln;
  216. context.result := Commands.CommandParseError;
  217. END;
  218. END;
  219. END UpdateBootFile;
  220. PROCEDURE GetConfig*(context : Commands.Context); (** dev#part *)
  221. CONST MaxSize = 2048;
  222. VAR
  223. getConfig : Lib.GetConfig;
  224. selection : Lib.Selection;
  225. configuration : Lib.Configuration;
  226. table : Streams.StringWriter;
  227. string : ARRAY MaxSize OF CHAR;
  228. i : LONGINT;
  229. BEGIN
  230. IF GetSelection(context, TRUE, selection) THEN
  231. NEW(getConfig, selection.disk, selection.partition, context.out);
  232. getConfig.SetBlockingStart;
  233. IF Lib.StatusError IN getConfig.state.status THEN
  234. context.result := Commands.CommandError;
  235. ELSE
  236. NEW(configuration);
  237. configuration.table := getConfig.GetTable();
  238. table := configuration.GetTableAsString();
  239. table.Get(string);
  240. (* Commands uses the quote character to separate commands *)
  241. FOR i := 0 TO LEN(string)-1 DO IF string[i] = ";" THEN string[i] := ","; END; END;
  242. context.out.String("Partitions.SetConfig "); context.out.String(getConfig.diskpartString); context.out.Ln;
  243. context.out.String(string); context.out.Ln;
  244. END;
  245. END;
  246. END GetConfig;
  247. (* Write the specified configuration string to the specified partition. *)
  248. (* Notes: *)
  249. (* - Use the "," character to separate commands *)
  250. (* - After an "#" character, the rest of the line is skipped/ignored *)
  251. PROCEDURE SetConfig*(context : Commands.Context); (** dev#part { str = "val" } ~ *)
  252. VAR
  253. setConfig : Lib.SetConfig;
  254. selection : Lib.Selection;
  255. configString : Strings.String;
  256. ch : CHAR;
  257. i : LONGINT;
  258. BEGIN
  259. IF GetSelection(context, TRUE, selection) THEN
  260. IF context.arg.CanSetPos() THEN
  261. (* append character "~" to config string *)
  262. NEW(configString, context.arg.Available() + 1);
  263. i := 0;
  264. WHILE (context.arg.Available() > 0) DO
  265. context.arg.Char(ch);
  266. IF (ch = ",") THEN
  267. configString[i] := ";"; (* Commands uses comma character to separate commands *)
  268. ELSE
  269. configString[i] := ch;
  270. END;
  271. INC(i);
  272. END;
  273. configString[i-1] := "~";
  274. configString[i] := 0X;
  275. NEW(setConfig, selection.disk, selection.partition, context.out);
  276. setConfig.SetParameters(configString, 0);
  277. setConfig.SetBlockingStart;
  278. ELSE
  279. context.error.String("Expected argument stream that supports SetPos"); context.error.Ln;
  280. context.result := Commands.CommandError;
  281. END;
  282. END;
  283. END SetConfig;
  284. (** Perform a read check on partition *)
  285. PROCEDURE Check*(context : Commands.Context); (** dev#part *)
  286. VAR
  287. selection : Lib.Selection;
  288. checkPartition : Lib.CheckPartition;
  289. BEGIN
  290. IF GetSelection(context, FALSE, selection) THEN
  291. NEW(checkPartition, selection.disk, selection.partition, context.out);
  292. checkPartition.SetBlockingStart;
  293. ELSE (* skip; error written to <w> by ScanOpenPart *)
  294. END;
  295. END Check;
  296. (** Change the type of dev#part from oldtype to newtype *)
  297. PROCEDURE ChangeType*(context : Commands.Context); (** dev#part oldtype newtype ~ *)
  298. VAR
  299. change : Lib.ChangePartType;
  300. oldtype, newtype : LONGINT;
  301. selection : Lib.Selection;
  302. BEGIN
  303. IF GetSelection(context, TRUE, selection) THEN
  304. IF ~selection.disk.isDiskette THEN
  305. IF context.arg.GetInteger(oldtype, FALSE) & (oldtype > 0) & (oldtype < 256) THEN
  306. IF context.arg.GetInteger(newtype, FALSE) & (newtype > 0) & (newtype < 256) THEN
  307. NEW(change, selection.disk, selection.partition, context.out);
  308. change.SetParameters(oldtype, newtype);
  309. change.SetBlockingStart;
  310. ELSE
  311. context.error.String("Expected parameters: dev#part oldtype newtype, failed to parse newtype"); context.error.Ln;
  312. context.result := Commands.CommandParseError;
  313. END;
  314. ELSE
  315. context.error.String("Expected parameters: dev#part oldtype newtype, failed to parse oldtype"); context.error.Ln;
  316. context.result := Commands.CommandParseError;
  317. END;
  318. ELSE
  319. context.error.String("Operation not support for floppy disk drives."); context.error.Ln;
  320. context.result := Commands.CommandError;
  321. END;
  322. END;
  323. END ChangeType;
  324. (** Delete a partition *)
  325. PROCEDURE Delete*(context : Commands.Context); (** dev#part type ~ *)
  326. VAR
  327. delete : Lib.DeletePartition;
  328. selection : Lib.Selection;
  329. type : LONGINT;
  330. BEGIN
  331. IF GetSelection(context, FALSE, selection) THEN
  332. IF ~selection.disk.isDiskette THEN
  333. IF context.arg.GetInteger(type, FALSE) & (type > 0) & (type < 256) THEN
  334. NEW(delete, selection.disk, selection.partition, context.out);
  335. delete.SetParameters(type);
  336. delete.SetBlockingStart;
  337. ELSE
  338. context.error.String("Expected parameters: dev#part type sizeMB, error while parsing type"); context.error.Ln;
  339. context.result := Commands.CommandParseError;
  340. END;
  341. ELSE
  342. context.error.String("Operation not supported for floppy disks"); context.error.Ln;
  343. context.result := Commands.CommandError;
  344. END;
  345. END;
  346. END Delete;
  347. PROCEDURE Create*(context : Commands.Context); (** dev#part type sizeMB ~ *)
  348. VAR
  349. create : Lib.CreatePartition;
  350. selection : Lib.Selection;
  351. type, size : LONGINT;
  352. BEGIN
  353. IF GetSelection(context, FALSE, selection) THEN
  354. IF ~selection.disk.isDiskette THEN
  355. IF context.arg.GetInteger(type, FALSE) & (type > 0) & (type < 256) THEN
  356. IF context.arg.GetInteger(size, FALSE) & (size > 0) THEN
  357. NEW(create, selection.disk, selection.partition, context.out);
  358. create.SetParameters(size, type, FALSE);
  359. create.SetBlockingStart;
  360. ELSE
  361. context.error.String("Expected parameters: dev#part type sizeMB, error while parsing size"); context.error.Ln;
  362. context.result := Commands.CommandParseError;
  363. END;
  364. ELSE
  365. context.error.String("Expected parameters: dev#part type sizeMB, error while parsing type"); context.error.Ln;
  366. context.result := Commands.CommandParseError;
  367. END;
  368. ELSE
  369. context.error.String("Operation not supported on floppy disks"); context.error.Ln;
  370. context.result := Commands.CommandError;
  371. END;
  372. END;
  373. END Create;
  374. (** Mark partition as active *)
  375. PROCEDURE Activate*(context : Commands.Context); (** dev#part ~ *)
  376. BEGIN
  377. ChangeActiveBit(TRUE, context);
  378. END Activate;
  379. (** Mark partition as inactive *)
  380. PROCEDURE Deactivate*(context : Commands.Context); (** dev#part ~ *)
  381. BEGIN
  382. ChangeActiveBit(FALSE, context);
  383. END Deactivate;
  384. PROCEDURE ChangeActiveBit(active : BOOLEAN; context : Commands.Context);
  385. VAR
  386. setFlags : Lib.SetFlags;
  387. selection : Lib.Selection;
  388. BEGIN
  389. IF GetSelection(context, TRUE, selection) THEN
  390. IF ~selection.disk.isDiskette THEN
  391. NEW(setFlags, selection.disk, selection.partition, context.out);
  392. setFlags.SetParameters(active);
  393. setFlags.SetBlockingStart;
  394. ELSE
  395. context.error.String("Operation not supported for floppy disks"); context.error.Ln;
  396. context.result := Commands.CommandError;
  397. END;
  398. END;
  399. END ChangeActiveBit;
  400. (** Write <numblock> sectors from a file to a partition, starting at block <block> *)
  401. PROCEDURE FileToPartition*(context : Commands.Context); (** dev#part filename [block numblocks] ~ *)
  402. VAR
  403. fileToPartition : Lib.FileToPartition;
  404. filename : ARRAY 128 OF CHAR;
  405. block, numblocks : LONGINT;
  406. selection : Lib.Selection;
  407. BEGIN
  408. IF GetSelection(context, TRUE, selection) THEN
  409. IF context.arg.GetString(filename) THEN
  410. IF context.arg.GetInteger(block, FALSE) THEN
  411. IF ~context.arg.GetInteger(numblocks, FALSE) THEN
  412. context.error.String("Exspected parameters: dev#part filename [block numblocks], failed to parse numblocks"); context.error.Ln;
  413. context.result := Commands.CommandParseError;
  414. RETURN;
  415. END;
  416. ELSE (* optional parameters not specified *)
  417. block := -1; numblocks := -1;
  418. END;
  419. NEW(fileToPartition, selection.disk, selection.partition, context.out);
  420. fileToPartition.SetParameters(filename, block, numblocks);
  421. fileToPartition.SetBlockingStart;
  422. ELSE
  423. context.error.String("Exspected parameters: dev#part name [block numblocks], failed to parse filename"); context.error.Ln;
  424. context.result := Commands.CommandParseError;
  425. END;
  426. END;
  427. END FileToPartition;
  428. (** Write <numblock> sectors from a partition to a file, starting at block <block>.
  429. If the optional parameters are not specified, store whole partition into file *)
  430. PROCEDURE PartitionToFile*(context : Commands.Context); (** dev#part filename [block numblocks] ~ *)
  431. VAR
  432. partitionToFile : Lib.PartitionToFile;
  433. filename : ARRAY 128 OF CHAR;
  434. block, numblocks : LONGINT;
  435. selection : Lib.Selection;
  436. BEGIN
  437. IF GetSelection(context, TRUE, selection) THEN
  438. IF context.arg.GetString(filename) THEN
  439. IF context.arg.GetInteger(block, FALSE) THEN
  440. IF ~context.arg.GetInteger(numblocks, FALSE) THEN
  441. context.error.String("Exspected parameters: dev#part filename [block numblocks], failed to parse numblocks"); context.error.Ln;
  442. context.result := Commands.CommandParseError;
  443. RETURN;
  444. END;
  445. ELSE (* optional parameters not specified *)
  446. block := -1; numblocks := -1;
  447. END;
  448. NEW(partitionToFile, selection.disk, selection.partition, context.out);
  449. partitionToFile.SetParameters(filename, block, numblocks);
  450. partitionToFile.SetBlockingStart;
  451. context.out.String("Partitions UID "); context.out.Int(partitionToFile.uid, 0); context.out.String(": Started PartitionToFile on ");
  452. context.out.String(partitionToFile.diskpartString); context.out.Ln;
  453. ELSE
  454. context.error.String("Exspected parameters: dev#part name [block numblocks], failed to parse filename"); context.error.Ln;
  455. context.result := Commands.CommandParseError;
  456. END;
  457. END;
  458. END PartitionToFile;
  459. (** Write the specified Master Boot Record (MBR) to the specified partition. The partition table will be preserved *)
  460. (* unless the optional parameter "DESTROY" is specified. *)
  461. (* WARNING: Using the DESTROY parameter will render any disk content unusable. *)
  462. PROCEDURE WriteMBR*(context : Commands.Context); (** dev#0 filename ["DESTROY"] ~ *)
  463. VAR
  464. writeMBR : Lib.WriteMBR;
  465. filename, destroy : ARRAY 128 OF CHAR;
  466. selection : Lib.Selection;
  467. BEGIN
  468. IF GetSelection(context, FALSE, selection) THEN
  469. IF ~selection.disk.isDiskette THEN
  470. IF selection.partition = 0 THEN
  471. IF context.arg.GetString(filename) THEN
  472. NEW(writeMBR, selection.disk, selection.partition, context.out);
  473. IF context.arg.GetString(destroy) & (destroy = "DESTROY") THEN
  474. writeMBR.SetParameters(filename, FALSE, FALSE);
  475. ELSE
  476. writeMBR.SetParameters(filename, TRUE, FALSE);
  477. END;
  478. writeMBR.SetBlockingStart;
  479. ELSE
  480. context.error.String("Expected parameters: dev#0 filename, failed to parse filename"); context.error.Ln;
  481. context.result := Commands.CommandParseError;
  482. END;
  483. ELSE
  484. context.error.String("Expected parameters: dev#0 filename, partition is not 0"); context.error.Ln;
  485. context.result := Commands.CommandParseError;
  486. END;
  487. ELSE
  488. context.error.String("Operation not supported for floppy disks"); context.error.Ln;
  489. context.result := Commands.CommandError;
  490. END;
  491. END;
  492. END WriteMBR;
  493. (** Update the boot loader OBL in an existing AosFS partition, replacing it by the new BBL handling the Init string differently.
  494. The BBL must imperatively have the same size, 4 blocks, as the OBL. The same BBL is applicable to all AosFS partitions. *)
  495. PROCEDURE UpdateBootLoader*(context : Commands.Context); (** dev#part BootLoader ~ *)
  496. VAR
  497. updateLoader : Lib.UpdateBootLoader;
  498. selection : Lib.Selection;
  499. filename : Files.FileName;
  500. BEGIN
  501. IF GetSelection(context, FALSE, selection) THEN
  502. IF context.arg.GetString(filename) THEN
  503. NEW(updateLoader, selection.disk, selection.partition, context.out);
  504. updateLoader.SetParameters(filename);
  505. updateLoader.SetBlockingStart;
  506. ELSE
  507. context.error.String("Expected parameters: dev#part bootloader"); context.error.Ln;
  508. context.result := Commands.CommandError;
  509. END;
  510. END;
  511. END UpdateBootLoader;
  512. (** Install boot manager on the specified device *)
  513. PROCEDURE InstallBootManager*(context : Commands.Context); (** dev#0 [BootManagerMBR [BootManagerTail]] ~ *)
  514. VAR installBootManager : Lib.InstallBootManager; selection : Lib.Selection; mbrFile, tailFile : Files.FileName;
  515. BEGIN
  516. IF GetSelection(context, FALSE, selection) THEN
  517. IF ~context.arg.GetString(mbrFile) THEN mbrFile := BootManagerMBRFile; END;
  518. IF ~context.arg.GetString(tailFile) THEN tailFile := BootManagerTailFile; END;
  519. NEW(installBootManager, selection.disk, selection.partition, context.out);
  520. installBootManager.SetParameters(mbrFile, tailFile);
  521. installBootManager.SetBlockingStart;
  522. END;
  523. END InstallBootManager;
  524. PROCEDURE ShowBlockCallback(text : Texts.Text);
  525. VAR string : Strings.String;
  526. BEGIN
  527. text.AcquireRead;
  528. NEW(string, text.GetLength()); TextUtilities.TextToStr(text, string^);
  529. text.ReleaseRead;
  530. KernelLog.String(string^); KernelLog.Ln;
  531. END ShowBlockCallback;
  532. PROCEDURE ShowBlocks*(context : Commands.Context); (** dev#part block [numblocks] ~ *)
  533. VAR
  534. showBlocks : Lib.ShowBlocks;
  535. block, numblocks : LONGINT;
  536. selection : Lib.Selection;
  537. BEGIN
  538. IF GetSelection(context, FALSE, selection) THEN
  539. IF context.arg.GetInteger(block, FALSE) THEN
  540. IF ~context.arg.GetInteger(numblocks, FALSE) THEN
  541. (* optional parameter not specified *) numblocks := -1;
  542. END;
  543. NEW(showBlocks, selection.disk, selection.partition, context.out);
  544. showBlocks.SetParameters(block, numblocks);
  545. showBlocks.SetCallback(ShowBlockCallback);
  546. showBlocks.SetBlockingStart;
  547. ELSE
  548. context.error.String("Exspected parameters: dev#part block [numblocks], failed to parse block"); context.error.Ln;
  549. context.result := Commands.CommandParseError;
  550. END;
  551. END;
  552. END ShowBlocks;
  553. (** Eject medium of device dev *)
  554. PROCEDURE Eject*(context : Commands.Context); (** dev ~ *)
  555. VAR
  556. plugin : Plugins.Plugin;
  557. dev : Disks.Device;
  558. name : ARRAY 32 OF CHAR;
  559. temp: ARRAY 256 OF CHAR;
  560. BEGIN
  561. IF context.arg.GetString(name) THEN
  562. plugin := Disks.registry.Get(name);
  563. IF plugin # NIL THEN
  564. dev := plugin (Disks.Device);
  565. Lib.Eject(dev, temp); context.out.String(temp); context.out.Ln;
  566. ELSE
  567. context.error.String("Device "); context.error.String(name); context.error.String(" not found"); context.error.Ln;
  568. context.result := Commands.CommandError;
  569. END;
  570. ELSE
  571. context.error.String("Expected parameters: dev"); context.error.Ln;
  572. context.result := Commands.CommandParseError;
  573. END;
  574. END Eject;
  575. (** Sync device to medium *)
  576. PROCEDURE Sync*(context: Commands.Context); (** dev ~ *)
  577. VAR
  578. plugin: Plugins.Plugin;
  579. dev: Disks.Device;
  580. name: ARRAY 32 OF CHAR;
  581. temp: ARRAY 256 OF CHAR;
  582. BEGIN
  583. IF context.arg.GetString(name) THEN
  584. plugin := Disks.registry.Get(name);
  585. IF plugin # NIL THEN
  586. dev := plugin (Disks.Device);
  587. Lib.Sync(dev, temp); context.out.String(temp); context.out.Ln;
  588. ELSE
  589. context.error.String("Device "); context.error.String(name); context.error.String(" not found"); context.error.Ln;
  590. context.result := Commands.CommandError;
  591. END;
  592. ELSE
  593. context.error.String("Expected parameters: dev"); context.error.Ln;
  594. context.result := Commands.CommandParseError;
  595. END;
  596. END Sync;
  597. PROCEDURE Unsafe*(context : Commands.Context); (** ~ *)
  598. BEGIN
  599. Lib.safe := FALSE;
  600. context.out.String("NOW in UNSAFE mode!"); context.out.Ln;
  601. END Unsafe;
  602. PROCEDURE Safe*(context : Commands.Context); (** ~ *)
  603. BEGIN
  604. Lib.safe := TRUE;
  605. context.out.String("Now in safe mode"); context.out.Ln;
  606. END Safe;
  607. (** Show all disk devices and their partition layout. *)
  608. PROCEDURE Show*(context : Commands.Context); (** ["detail"] ~ *)
  609. VAR
  610. diskTable : Lib.Disks; disk : Lib.Disk;
  611. par : ARRAY 10 OF CHAR;
  612. verbose : BOOLEAN;
  613. i : LONGINT;
  614. temp : ARRAY 256 OF CHAR;
  615. BEGIN
  616. par := ""; context.arg.SkipWhitespace; context.arg.String(par);
  617. verbose := (par = "detail");
  618. Lib.diskModel.Update;
  619. Lib.diskModel.Acquire;
  620. diskTable := Lib.diskModel.disks;
  621. IF diskTable # NIL THEN
  622. FOR i := 0 TO LEN(diskTable)-1 DO
  623. disk := diskTable[i];
  624. ShowDevice(context, disk, verbose);
  625. IF disk.res # Disks.MediaMissing THEN
  626. IF (disk.table # NIL) THEN
  627. ShowTable(context, disk, disk.table, verbose)
  628. ELSE
  629. Lib.GetErrorMsg("Error", disk.res, temp); context.error.String(temp); context.error.Ln;
  630. END
  631. END;
  632. context.error.Ln;
  633. END;
  634. ELSE
  635. context.error.String("No Devices found"); context.error.Ln;
  636. context.result := Commands.CommandError;
  637. END;
  638. Lib.diskModel.Release;
  639. END Show;
  640. PROCEDURE ShowDevice(context : Commands.Context; disk: Lib.Disk; verbose: BOOLEAN);
  641. VAR temp: ARRAY 256 OF CHAR;
  642. BEGIN
  643. context.out.String("Disk: "); context.out.String(disk.device.name); context.out.String(", ");
  644. IF disk.res = Disks.Ok THEN
  645. Lib.WriteK(context.out, ENTIER(disk.size * 1.0 * disk.device.blockSize / 1024));
  646. IF verbose THEN
  647. context.out.String(" = "); context.out.Int(disk.size, 1);
  648. context.out.String(" * "); context.out.Int(disk.device.blockSize,1);
  649. END
  650. ELSE
  651. Lib.GetErrorMsg("GetSize failed", disk.res, temp); context.error.String(temp);
  652. END;
  653. IF Disks.Removable IN disk.device.flags THEN context.out.String(", removable") END;
  654. IF Disks.ReadOnly IN disk.device.flags THEN context.out.String(", read-only") END;
  655. IF verbose THEN
  656. IF disk.res # Disks.MediaMissing THEN
  657. context.out.String(", ");
  658. IF disk.gres = Disks.Ok THEN
  659. context.out.String("CHS: "); context.out.Int(disk.geo.cyls, 1); context.out.String("*");
  660. context.out.Int(disk.geo.hds, 1); context.out. String("*"); context.out.Int(disk.geo.spt, 1);
  661. ELSE
  662. Lib.GetErrorMsg("GetCHS: ", disk.gres, temp); context.error.String(temp);
  663. END
  664. END
  665. END;
  666. IF disk.device.desc # "" THEN context.out.String(", "); context.out.String(disk.device.desc) END;
  667. IF verbose THEN context.out.String(", mntcnt="); context.out.Int(disk.device.openCount, 1) END;
  668. context.out.Ln;
  669. END ShowDevice;
  670. PROCEDURE ShowTable( context : Commands.Context; disk: Lib.Disk; table: Disks.PartitionTable; verbose: BOOLEAN);
  671. VAR j: LONGINT; r: LONGREAL; ugly : ARRAY 10 OF CHAR; temp : ARRAY 128 OF CHAR; ignore : LONGINT;
  672. BEGIN
  673. FOR j := 0 TO LEN(table)-1 DO
  674. r := (table[j].size * 1.0D0 * disk.device.blockSize) / (1024*1024); (* M *)
  675. Lib.WritePart(context.out, disk.device, j);
  676. IF verbose THEN
  677. context.out.Int(table[j].start, 12);
  678. context.out.Int(table[j].size, 12)
  679. END;
  680. Strings.FloatToStr(r, 6, 1, 0, ugly);
  681. IF r < 10 THEN context.out.String(ugly);
  682. ELSE context.out.Int(ENTIER(r), 6)
  683. END;
  684. context.out.String(" MB ");
  685. IF (table[j].type >= 1) & (table[j].type <= 255) THEN
  686. context.out.Int(table[j].type, 3)
  687. ELSE
  688. context.out.String("---")
  689. END;
  690. context.out.Char(" ");
  691. IF (j # 0) & ~(Disks.Primary IN table[j].flags) THEN context.out.String( " | ") END; (* logical drive *)
  692. IF Disks.Boot IN table[j].flags THEN context.out.String(" * ") END; (* bootable *)
  693. Lib.WriteType(table[j].type, temp, ignore); context.out.String(temp);
  694. IF verbose THEN
  695. IF Disks.Mounted IN table[j].flags THEN context.out.String(" [mounted]") END
  696. END;
  697. context.out.Ln;
  698. END
  699. END ShowTable;
  700. (** Display limitations of AosFS *)
  701. PROCEDURE ShowAosFSLimits*(context : Commands.Context); (** ~ *)
  702. BEGIN
  703. Lib.ShowAosFSLimits;
  704. END ShowAosFSLimits;
  705. PROCEDURE UpdateDiskModel*(context : Commands.Context);(** ~ *)
  706. BEGIN
  707. Lib.diskModel.Update;
  708. context.out.String("Disk model updated."); context.out.Ln;
  709. END UpdateDiskModel;
  710. (* Scan the command line parameters for a device#partition specification. *)
  711. (* The Writer <w> is used to return error messages, <r> contains p.str (dev#part skipped) *)
  712. (* check : IF TRUE, only Disks.Valid partitions are returns *)
  713. PROCEDURE GetSelection*(context : Commands.Context; check : BOOLEAN; VAR selection : Lib.Selection) : BOOLEAN;
  714. VAR devpart : ARRAY 32 OF CHAR;
  715. BEGIN
  716. selection.disk.device := NIL; selection.partition := -1; (* invalid *)
  717. IF ~context.arg.GetString(devpart) THEN
  718. context.error.String("Expected parameters: dev#part"); context.error.Ln; context.error.Update;
  719. context.result := Commands.CommandParseError;
  720. RETURN FALSE;
  721. END;
  722. context.arg.SkipWhitespace;
  723. (* special case: diskette *)
  724. IF Strings.Match("Diskette*", devpart) THEN
  725. check := FALSE;
  726. END;
  727. Lib.diskModel.Update;
  728. IF Lib.diskModel.GetDisk(devpart, selection, check) THEN
  729. IF Trace THEN
  730. KernelLog.String("Partitions: Command line selection: "); KernelLog.String(selection.disk.device.name);
  731. KernelLog.Char("#"); KernelLog.Int(selection.partition, 0); KernelLog.Ln;
  732. END;
  733. RETURN TRUE;
  734. ELSE
  735. context.error.String("Partition "); context.error.String(devpart); context.out.String(" not found."); context.error.Ln;
  736. context.error.Update; context.result := Commands.CommandError;
  737. END;
  738. RETURN FALSE;
  739. END GetSelection;
  740. END Partitions.
  741. System.Free DiskBenchmark Partitions ~