Skip to content

Commit

Permalink
feat(example): improvements to allow flashing files
Browse files Browse the repository at this point in the history
  • Loading branch information
r3pwn committed Aug 30, 2023
1 parent e550fc8 commit c9e1966
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
39 changes: 16 additions & 23 deletions example/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<script setup lang="ts">
import { ref } from 'vue';
import libmjolnir, { OdinDevice, libpit } from 'libmjolnir';
// allowlist for erasable partitions to avoid letting a user mess up their device
const ALLOW_ERASE = ['cache', 'userdata'];
import libmjolnir, { OdinDevice, libpit } from '../..';
import PartitionEntry from './components/PartitionEntry.vue';
const hasDevice = ref(false);
const pitEntries = ref([] as libpit.PitEntry[]);
const devicePit = ref(undefined as libpit.PitData | undefined);
const connectedDevice = ref({} as OdinDevice);
async function setupDevice (device: OdinDevice) {
Expand All @@ -17,7 +15,7 @@
device.onDisconnect(() => {
hasDevice.value = false;
pitEntries.value = [];
devicePit.value = undefined;
console.log('device was disconnected')
});
}
Expand All @@ -42,12 +40,12 @@
async function receivePitFile () {
await connectedDevice.value.getPitData().then((pitData) => {
console.log(pitData);
pitEntries.value = pitData.entries;
devicePit.value = pitData;
});
}
async function erasePartition (partitionName: string) {
await connectedDevice.value.erasePartition(partitionName);
async function flashPartition (data: {name: string, data: Uint8Array}) {
await connectedDevice.value.flashPartition(data.name, data.data);
}
</script>

Expand All @@ -59,20 +57,15 @@
<button @click="requestDeviceType">Request device type</button>
<button @click="receivePitFile">Print PIT file</button>

<p v-if="pitEntries.length">
<p v-for="entry in pitEntries">
<div>partitionName: {{ entry.partitionName }}</div>
<div>identifier: {{ entry.identifier }}</div>
<div>flashFileName: {{ entry.flashFilename }}</div>
<div>blockSizeOrOffset: {{ entry.blockSizeOrOffset }}</div>
<button
v-if="entry.isFlashable() && ALLOW_ERASE.includes(entry.partitionName.toLowerCase())"
@click="erasePartition(entry.partitionName)"
>
Erase
</button>
<div>----------------------------------------------</div>
</p>
<p v-if="devicePit?.entries?.length">
<div>board type: {{ devicePit.pitName }}</div>
<template v-for="(entry, index) in devicePit.entries">
<partition-entry
:entry="entry"
@flash="flashPartition"
/>
<hr v-if="index !== devicePit.entries.length - 1" />
</template>
</p>
</p>
</template>
Expand Down
48 changes: 48 additions & 0 deletions example/src/components/PartitionEntry.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script setup lang="ts">
import { ref } from 'vue';
import { libpit } from 'libmjolnir';
const props = defineProps({
entry: {
type: libpit.PitEntry,
required: true
}
});
const emit = defineEmits(['flash']);
const currentFile = ref(undefined as File | undefined)
function stageFile (event: any) {
currentFile.value = event?.target?.files?.[0];
}
async function flashPartition (partitionName: string) {
if (!currentFile.value) {
return;
}
emit('flash', {
name: partitionName,
data: new Uint8Array(await currentFile.value.arrayBuffer())
});
}
</script>

<template>
<p class="pit-entry">
<div>partitionName: {{ entry.partitionName }}</div>
<div>identifier: {{ entry.identifier }}</div>
<div>flashFileName: {{ entry.flashFilename }}</div>
<div>blockSizeOrOffset: {{ entry.blockSizeOrOffset }}</div>
<template v-if="entry.isFlashable()">
<input type="file" :id="`flash-${entry.identifier}`" @change="stageFile"/>
<button
:disabled="!currentFile"
@click="flashPartition(entry.partitionName)"
>
Flash partition
</button>
</template>
</p>
</template>

0 comments on commit c9e1966

Please sign in to comment.