4
0

sbi-version.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //! This example illustrates how to use the `Version` structure which represents a valid
  2. //! RISC-V SBI version.
  3. /// Import the version type defined in the SBI specification, used for representing and
  4. /// manipulating SBI version numbers.
  5. use sbi_spec::base::Version;
  6. /// We mock a S-mode software (kernel, etc.) that runs on minimum SBI version of v1.0;
  7. /// it will detect from the environment and judge if it meets the minimum SBI version demand.
  8. fn main() {
  9. // Create a version structure representing the minimum version at RISC-V SBI v1.0.
  10. let minimum_version = Version::V1_0;
  11. // Call the mock SBI runtime to obtain the current version number.
  12. println!("Probing SBI version of current environment...");
  13. let current_version = sbi::get_spec_version();
  14. // Print the detected SBI version.
  15. println!("Kernel running on SBI version {}", current_version);
  16. // Version comparison: Check whether the current version meets the minimum
  17. // requirement (v1.0 or higher).
  18. if current_version >= minimum_version {
  19. // The version meets the requirement, output success message.
  20. println!("The SBI version meets minimum demand of RISC-V SBI v1.0.");
  21. println!("✓ Test success!");
  22. } else {
  23. // The version is too low, output failure message.
  24. println!("✗ Test failed, SBI version is too low.");
  25. }
  26. }
  27. /* -- Implementation of a mock SBI runtime -- */
  28. /// Module simulating an SBI runtime for the test environment.
  29. mod sbi {
  30. use sbi_spec::base::Version;
  31. /// Mock function to retrieve the SBI specification version.
  32. pub fn get_spec_version() -> Version {
  33. // Return a hardcoded version number `0x0200_0000`.
  34. // Using the parsing rule from the RISC-V SBI Specification, this represents major
  35. // version 2, minor version 0 (i.e., 2.0).
  36. // In a real environment, this function should interact with the SBI implementation
  37. // via ECALL to obtain the actual version.
  38. Version::from_raw(0x0200_0000)
  39. }
  40. }
  41. /* Code execution result analysis:
  42. The current simulated SBI version is 2.0, which is higher than the minimum requirement
  43. of 1.0, so the output will be:
  44. ✓ Test success!
  45. To test a failure scenario, modify the mock return value to a version lower than 1.0,
  46. for example:
  47. `Version::from_raw(0x0000_0002)` represents 0.2.
  48. */