How to Overwrite the firmware of a USB drive? - firmware

If a USB stick has its firmware stored in a re-writable memory, can I overwrite it? If yes, then how? I want to program a USB drive that whenever plugged in will automatically change the device wallpaper to a predefined one.

Related

Armbian - how to turn off a hard drive connected on SATA

I have an Orange Pi + 2 single board computer with the ability to connect an external hard drive via SATA.
Armbian is installed on the hard disk, the bootloader is in the onboard memory of the board.
Any command to turn off the device leads to the same result: the indicators on the board go out, it is no longer available via SSH, but the hard disk, any devices connected via USB continue to work, and the image via HDMI is also displayed on the screen.
How to completely turn off the device so that the hard drive stops correctly?
I believe that if you are capable of powering the device off using the operating system's menu, then it should be safe to cut the power off from the device itself. I have a raspberry pi 4 and have a little USB-C adapter that has a simple power switch which makes this concept very convenient for me. Cutting power to a turned-on, working device is very dangerous as it can damage critical files that could be being written. For this reason, it is probably smart to back up the device and also make sure that you are powering the device off through the operating system so that it can finish editing core files in case it needs to.

Is it possible to write a program on flash drive (pen drive) to perform a task on inserting flash drive into USB port?

I have a music playlist folder in my Flash drive (pen drive), music names are sorted in a fixed manner, I want to shuffle my music playlist by renaming the files. Generally I use Flash drive to play music in my car.
Is it practically possible to write a program for a flash drive? On inserting a flash drive into PC/car firmware; music files in the playlist folder must be shuffled every time.
Thank you.
I searched on web, I didn't find useful resources to accomplish the task.
It's only possible if either of the following are true:
The "computer" you're inserting it into supports running code. (Like an autoexec.bat on Windows). Obviously this depends entirely on what CPU/"OS" your car's audio player runs. Unlikely that there's any intentional support for executing code, but you might find an exploit that would let you run code on your car's audio-player microcontroller. The audio player probably doesn't really have an OS, it's probably just a lightweight custom system.
Your flash drive can run custom firmware that has a driver for FAT32 and modifies its own contents on powerup, before making itself visible to the host.
Flash drives do have a microcontroller internally to run the flash remapping / wear-leveling firmware. But usually there's no documented interface for uploading programs to it. And normally it doesn't know anything about filesystems, only block-level stuff, so anything you wanted to stuff into it would have to include a driver for FAT32.
I assume some people have reverse-engineered the programming / firmware-update interface on some flash drives.
You can probably also get USB devices that are designed to run custom programs like this as well as act as USB storage. If you really really want this (and your car doesn't have a "randomize" mode you can use instead), buying a USB-storage device that was designed to be programmable would probably be the easiest way to go. I assume such things exist but IDK.

how the usb spi flashing device write program to atmega?

the atmega microcontrollers have the internal programmable memory.So the usb flash device write data to internal memory of atmega through spi or it store the program and works as slave?i also want to know can the atmega get program,file in the MicroSD and run without internal memory or it need to run a program in internal disk then including the program in MicroSD?i don't know how the process work in atmega microcontroller.
There are several ways to program the AVR:
Using an external programmer. The Chip is in reset state during this. Then the programmer uses special a special protocol to directly write to the flash inside the controller. This is how your USB programming device probably writes code to the AVR. There are several protocols for this, but the most common one uses the same pins that are used by SPI. You should not confuse them just because of that ;-).
The controller writes the program itself. The chip is actively executing a program. That program on the chip fetches the instructions from e.g. a sd card and using the SPM instruction to copy it to local flash.
The AVR can only fetch instructions from it's own flash memory - you cannot execute directly from RAM or from any other external source.
The best answer for this question you can find at the end or middle of datasheet for AVR device (eg ATMEGA 328P-PU). If you are using some flashing device connected with computer, there is some initialization sequence after start and then there are send some 4 Bytes commands that tels microcontrolers, where to store values to flash and/or eeprom, fuse bits, or cares about erasing etc. There is also posible to read ID of chip.
Atmega microcontroler can be also programmed in paralel mode, where are some bits used for sending commands that tels, what to do with data on data bit.
If you ask about downloading program from SD card or any other device, there is possible to write to the flash through SPM, as there was told above. Some devices do not have part of flash for downloader - if I remember ATMEGA 48, and devices, that have configurable part of memory usable also for downloader, that can handle this, eg. ATMEGA 88, 168, 328. So if you want to download program from SPI, USART, TWI, etc, you need to configure fuse bits and create downloader that will do it for you. I am not sure, how ATMEGA 48 do this, but there si probably possible to write whole flash by SPM instruction.
However the best answer you can find in the datasheet. On pages folowing 255 you can find further information. http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf

How to add a serial number to my usb stick

I have a cheap usb stick. Unfortunately my stick has no serial number.
Is it possible to add an own serial number to my stick?
No, this is not possible in general. To change the USB descriptors and add a USB serial number, you would have to modify the firmware of the USB stick. It's unlikely that the procedure for writing to it would be documented, or that the firmware itself would be available in source form. The effort it takes to figure that stuff out probably outweighs the cost of just buying a better USB device.
Also, why do you need a serial number? Perhaps you can store a UUID of some sort in the filesystem of the USB drive instead of in the firmware of the drive itself.
However, this link about reprogramming USB devices might be useful:
https://nakedsecurity.sophos.com/2014/10/06/badusb-now-with-do-it-yourself-instructions/

USB in an embedded system without RTOS

I have no experience of embedded USB stacks so my question is, can I run it without an OS?
Of course it must be possible to run without OS, but will things be MUCH easier if I have one?
I want to use it to save data to a attached USB Mass Storage Device.
If your USB device is on-chip, your chip vendor will almost certainly have example code for USB that may include mass storage. You won't need an OS, but interrupt handling will be necessary and a file system too.
Your USB controller will need host or OTG capability - if it is only device capable, then you cannot connect to another USB device, only a host.
The benefit of an OS - or at least a simple RTOS kernel - is that you can schedule file system activity concurrently with other processing tasks. The OS in that case would not necessarily make things easier, but it may make your system more responsive to critical tasks and events.
I have used usb stacks in the past with PIC18F2550 (8 bits) and LPC1343 (32 bits ARM-Cortex M3) microcontrollers without any problems.