1ba991379Sopenharmony_ci/* 2ba991379Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3ba991379Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4ba991379Sopenharmony_ci * you may not use this file except in compliance with the License. 5ba991379Sopenharmony_ci * You may obtain a copy of the License at 6ba991379Sopenharmony_ci * 7ba991379Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8ba991379Sopenharmony_ci * 9ba991379Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10ba991379Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11ba991379Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12ba991379Sopenharmony_ci * See the License for the specific language governing permissions and 13ba991379Sopenharmony_ci * limitations under the License. 14ba991379Sopenharmony_ci */ 15ba991379Sopenharmony_ci 16ba991379Sopenharmony_cipackage dayu200 17ba991379Sopenharmony_ci 18ba991379Sopenharmony_ciimport ( 19ba991379Sopenharmony_ci "context" 20ba991379Sopenharmony_ci "errors" 21ba991379Sopenharmony_ci "fmt" 22ba991379Sopenharmony_ci "fotff/utils" 23ba991379Sopenharmony_ci "github.com/sirupsen/logrus" 24ba991379Sopenharmony_ci "os" 25ba991379Sopenharmony_ci "path/filepath" 26ba991379Sopenharmony_ci "regexp" 27ba991379Sopenharmony_ci "strings" 28ba991379Sopenharmony_ci "time" 29ba991379Sopenharmony_ci) 30ba991379Sopenharmony_ci 31ba991379Sopenharmony_civar partList = []string{"boot_linux", "system", "vendor", "userdata", "resource", "ramdisk", "chipset", "sys-prod", "chip-prod", "updater"} 32ba991379Sopenharmony_ci 33ba991379Sopenharmony_ci// All timeouts are calculated on normal cases, we do not certain that timeouts are enough if some sleeps canceled. 34ba991379Sopenharmony_ci// So simply we do not cancel any Sleep(). TODO: use utils.SleepContext() instead. 35ba991379Sopenharmony_cifunc (m *Manager) flashDevice(device string, pkg string, ctx context.Context) error { 36ba991379Sopenharmony_ci if err := utils.TryRebootToLoader(device, ctx); err != nil { 37ba991379Sopenharmony_ci return err 38ba991379Sopenharmony_ci } 39ba991379Sopenharmony_ci if err := m.flashImages(device, pkg, ctx); err != nil { 40ba991379Sopenharmony_ci return err 41ba991379Sopenharmony_ci } 42ba991379Sopenharmony_ci time.Sleep(20 * time.Second) // usually, it takes about 20s to reboot into OpenHarmony 43ba991379Sopenharmony_ci if connected := utils.WaitHDC(device, ctx); !connected { 44ba991379Sopenharmony_ci logrus.Errorf("flash device %s done, but boot unnormally, hdc connection fail", device) 45ba991379Sopenharmony_ci return fmt.Errorf("flash device %s done, but boot unnormally, hdc connection fail", device) 46ba991379Sopenharmony_ci } 47ba991379Sopenharmony_ci time.Sleep(10 * time.Second) // wait 10s more to ensure system has been started completely 48ba991379Sopenharmony_ci logrus.Infof("flash device %s successfully", device) 49ba991379Sopenharmony_ci return nil 50ba991379Sopenharmony_ci} 51ba991379Sopenharmony_ci 52ba991379Sopenharmony_cifunc (m *Manager) flashImages(device string, pkg string, ctx context.Context) error { 53ba991379Sopenharmony_ci logrus.Infof("calling flash tool to flash %s into %s...", pkg, device) 54ba991379Sopenharmony_ci locationID := m.locations[device] 55ba991379Sopenharmony_ci if locationID == "" { 56ba991379Sopenharmony_ci data, _ := utils.ExecCombinedOutputContext(ctx, m.FlashTool, "LD") 57ba991379Sopenharmony_ci locationID = strings.TrimPrefix(regexp.MustCompile(`LocationID=\d+`).FindString(string(data)), "LocationID=") 58ba991379Sopenharmony_ci if locationID == "" { 59ba991379Sopenharmony_ci time.Sleep(5 * time.Second) 60ba991379Sopenharmony_ci data, _ := utils.ExecCombinedOutputContext(ctx, m.FlashTool, "LD") 61ba991379Sopenharmony_ci locationID = strings.TrimPrefix(regexp.MustCompile(`LocationID=\d+`).FindString(string(data)), "LocationID=") 62ba991379Sopenharmony_ci } 63ba991379Sopenharmony_ci } 64ba991379Sopenharmony_ci logrus.Infof("locationID of %s is [%s]", device, locationID) 65ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, m.FlashTool, "-s", locationID, "UL", filepath.Join(m.Workspace, pkg, "MiniLoaderAll.bin"), "-noreset"); err != nil { 66ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 67ba991379Sopenharmony_ci return err 68ba991379Sopenharmony_ci } 69ba991379Sopenharmony_ci logrus.Errorf("flash MiniLoaderAll.bin fail: %v", err) 70ba991379Sopenharmony_ci time.Sleep(5 * time.Second) 71ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, m.FlashTool, "-s", locationID, "UL", filepath.Join(m.Workspace, pkg, "MiniLoaderAll.bin"), "-noreset"); err != nil { 72ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 73ba991379Sopenharmony_ci return err 74ba991379Sopenharmony_ci } 75ba991379Sopenharmony_ci logrus.Errorf("flash MiniLoaderAll.bin fail: %v", err) 76ba991379Sopenharmony_ci return err 77ba991379Sopenharmony_ci } 78ba991379Sopenharmony_ci } 79ba991379Sopenharmony_ci time.Sleep(3 * time.Second) 80ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, m.FlashTool, "-s", locationID, "DI", "-p", filepath.Join(m.Workspace, pkg, "parameter.txt")); err != nil { 81ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 82ba991379Sopenharmony_ci return err 83ba991379Sopenharmony_ci } 84ba991379Sopenharmony_ci logrus.Errorf("flash parameter.txt fail: %v", err) 85ba991379Sopenharmony_ci return err 86ba991379Sopenharmony_ci } 87ba991379Sopenharmony_ci time.Sleep(5 * time.Second) 88ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, m.FlashTool, "-s", locationID, "DI", "-uboot", filepath.Join(m.Workspace, pkg, "uboot.img"), filepath.Join(m.Workspace, pkg, "parameter.txt")); err != nil { 89ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 90ba991379Sopenharmony_ci return err 91ba991379Sopenharmony_ci } 92ba991379Sopenharmony_ci logrus.Errorf("flash device fail: %v", err) 93ba991379Sopenharmony_ci return err 94ba991379Sopenharmony_ci } 95ba991379Sopenharmony_ci time.Sleep(5 * time.Second) 96ba991379Sopenharmony_ci for _, part := range partList { 97ba991379Sopenharmony_ci if _, err := os.Stat(filepath.Join(m.Workspace, pkg, part+".img")); err != nil { 98ba991379Sopenharmony_ci if os.IsNotExist(err) { 99ba991379Sopenharmony_ci logrus.Infof("part %s.img not exist, ignored", part) 100ba991379Sopenharmony_ci continue 101ba991379Sopenharmony_ci } 102ba991379Sopenharmony_ci return err 103ba991379Sopenharmony_ci } 104ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, m.FlashTool, "-s", locationID, "DI", "-"+part, filepath.Join(m.Workspace, pkg, part+".img"), filepath.Join(m.Workspace, pkg, "parameter.txt")); err != nil { 105ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 106ba991379Sopenharmony_ci return err 107ba991379Sopenharmony_ci } 108ba991379Sopenharmony_ci logrus.Errorf("flash device fail: %v", err) 109ba991379Sopenharmony_ci logrus.Warnf("try again...") 110ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, m.FlashTool, "-s", locationID, "DI", "-"+part, filepath.Join(m.Workspace, pkg, part+".img"), filepath.Join(m.Workspace, pkg, "parameter.txt")); err != nil { 111ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 112ba991379Sopenharmony_ci return err 113ba991379Sopenharmony_ci } 114ba991379Sopenharmony_ci logrus.Errorf("flash device fail: %v", err) 115ba991379Sopenharmony_ci return err 116ba991379Sopenharmony_ci } 117ba991379Sopenharmony_ci } 118ba991379Sopenharmony_ci time.Sleep(3 * time.Second) 119ba991379Sopenharmony_ci } 120ba991379Sopenharmony_ci time.Sleep(5 * time.Second) // sleep a while for writing 121ba991379Sopenharmony_ci if err := utils.ExecContext(ctx, m.FlashTool, "-s", locationID, "RD"); err != nil { 122ba991379Sopenharmony_ci if errors.Is(err, context.Canceled) { 123ba991379Sopenharmony_ci return err 124ba991379Sopenharmony_ci } 125ba991379Sopenharmony_ci return fmt.Errorf("reboot device fail: %v", err) 126ba991379Sopenharmony_ci } 127ba991379Sopenharmony_ci return nil 128ba991379Sopenharmony_ci} 129