I FINALLY SOLVED IT!!
After nearly 2 years of headache I finally got it through without resetting the system (by the way thank you MS for doing nothing)
It's basically some kind of conflict between the upgrade and your sata driver, all you need to do is set your sata driver from AHCI to IDE before upgrading to 8.1, but switch from AHCI to IDE in Windows 8 takes bit of a hassle.
Now read *carefully* in these two links (a big thank you to those guys)
http://reboot.pro/topic/19467-booting-win-881-in-ide-or-ahci-mode-by-only-changing-bios-setting/
I believe it will solve 0x40017 which bothers you for so long, and don't forget to switch back to AHCI after the 8.1 upgrade is done, good luck!