This question already has an answer here:
I'm planning to buy a second hand computer (2012 Mac mini, which is no longer available in stores). Because I'm not sure if there is any malware in the computer so I will perform hard format several times. I think this could prevent malwares to exist in the drive. But I'm not sure if this is enough for ensure the safety because there could be other problem like other have written malicious programs in the firmware etc. Is there any way to ensure a second hand computer is safe to use?