Add firmware upgrading logic and fix some bugs.