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