def test_failed_mount(self): # set up a filesystem first with sdcard.filesystem(mounted=False): fatfs.mkfs() with sdcard.filesystem(): self.assertTrue(fatfs.is_mounted()) # trash filesystem io.sdcard.power_on() io.sdcard.write(0, bytes([0xFF] * io.sdcard.BLOCK_SIZE)) io.sdcard.power_off() # mounting should now fail with self.assertRaises(OSError): with sdcard.filesystem(): pass self.assertFalse(fatfs.is_mounted()) # it should be possible to create an unmounted instance with sdcard.filesystem(mounted=False): fatfs.mkfs() # mounting should now succeed with sdcard.filesystem(): self.assertTrue(fatfs.is_mounted())
def test_mount(self): # set up a filesystem first with sdcard.filesystem(mounted=False): fatfs.mkfs() with sdcard.filesystem(): self.assertTrue(fatfs.is_mounted()) self.assertFalse(fatfs.is_mounted())
def test_mount_nomount(self): with self.assertRaises(RuntimeError): with sdcard.filesystem(mounted=True): with sdcard.filesystem(mounted=False): pass with self.assertRaises(RuntimeError): with sdcard.filesystem(mounted=False): with sdcard.filesystem(mounted=True): pass
def test_power(self): # sdcard.capacity() will return 0 if the card is not powered, # non-zero value otherwise self.assertEqual(sdcard.capacity(), 0) with sdcard.filesystem(mounted=False): self.assertTrue(sdcard.capacity() > 0) self.assertEqual(sdcard.capacity(), 0)
async def ensure_sdcard(ctx: wire.GenericContext, ensure_filesystem: bool = True) -> None: """Ensure a SD card is ready for use. This function runs the UI flow needed to ask the user to insert a SD card if there isn't one. If `ensure_filesystem` is True (the default), it also tries to mount the SD card filesystem, and allows the user to format the card if a filesystem cannot be mounted. """ while not sdcard.is_present(): if not await insert_card_dialog(ctx): raise SdCardUnavailable("SD card required.") if not ensure_filesystem: return while True: try: try: with sdcard.filesystem(mounted=False): fatfs.mount() except fatfs.NoFilesystem: # card not formatted. proceed out of the except clause pass else: # no error when mounting return if not await format_card_dialog(ctx): raise SdCardUnavailable("SD card not formatted.") # Proceed to formatting. Failure is caught by the outside OSError handler with sdcard.filesystem(mounted=False): fatfs.mkfs() fatfs.mount() fatfs.setlabel("TREZOR") # format and mount succeeded return except OSError: # formatting failed, or generic I/O error (SD card power-on failed) if not await sd_problem_dialog(ctx): raise SdCardUnavailable("Error accessing SD card.")
async def ensure_sdcard( ctx: wire.GenericContext, ensure_filesystem: bool = True ) -> None: """Ensure a SD card is ready for use. This function runs the UI flow needed to ask the user to insert a SD card if there isn't one. If `ensure_filesystem` is True (the default), it also tries to mount the SD card filesystem, and allows the user to format the card if a filesystem cannot be mounted. """ while not sdcard.is_present(): if not await insert_card_dialog(ctx): raise SdCardUnavailable("SD card required.") if not ensure_filesystem: return while True: try: with sdcard.filesystem(mounted=False): fatfs.mount() # Mount succeeded, filesystem is OK return except OSError: # Mount failed. Handle the problem outside except-clause pass if not await format_card_dialog(ctx): raise SdCardUnavailable("SD card not formatted.") try: with sdcard.filesystem(mounted=False): fatfs.mkfs() fatfs.mount() fatfs.setlabel("TREZOR") # mkfs and mount succeeded return except OSError: pass # allow retry if we get as far as here if not await sd_problem_dialog(ctx): raise SdCardUnavailable("Problem formatting SD card.")
def test_nesting(self): # set up a filesystem first with sdcard.filesystem(mounted=False): fatfs.mkfs() self.assertEqual(sdcard.capacity(), 0) with sdcard.filesystem(): self.assertTrue(sdcard.capacity() > 0) self.assertTrue(fatfs.is_mounted()) with sdcard.filesystem(): self.assertTrue(sdcard.capacity() > 0) self.assertTrue(fatfs.is_mounted()) self.assertTrue(sdcard.capacity() > 0) self.assertTrue(fatfs.is_mounted()) self.assertEqual(sdcard.capacity(), 0) self.assertFalse(fatfs.is_mounted())
def test_nomount(self): with sdcard.filesystem(mounted=False): self.assertFalse(fatfs.is_mounted())